9 Replies
      Latest reply: Sep 20, 2016 2:17 PM by ak1982 RSS
      ak1982 Level 1 Level 1 (0 points)

        Hello,

         

        I have a combobox and I have to possibilitys (Yes and No). Now I want to check if one item is used. In the moment my If Condition is nil :-(

         

        My code:

         

            @IBOutlet weak var fruehbucher: NSComboBox!

             var fruehbucherBetragFinal:Float?

         

            @IBAction func conditionFruehbucher(sender:NSComboBox){

                if fruehbucher.stringValue == "Ja" {

                    var fruehbucherBetrag = ((Float(MesseA.integerValue) * MesseAPreis!)+(Float(MesseB.integerValue) * MesseBPreis!)+(Float(MesseC.integerValue) * MesseCPreis!) + (Float(MesseD.integerValue) * MesseDPreis!)+(Float(MesseE.integerValue) * MesseEPreis!))

                    var fruehbucherBetragFinal = (fruehbucherBetrag * 0,03)

                }

            

            }

         

        Can anybody help me

        Greetings

        Andreas

        • Re: How to check value of NSCombox
          ak1982 Level 1 Level 1 (0 points)

          I work with to internal Items 1.) Ja und 2.) Nein

          • Re: How to check value of NSCombox
            QuinceyMorris Level 6 Level 6 (2,465 points)

            Are you saying that the "if" condition is false, or that "fruehbucher.stringValue" is nil or the empty string? (What is the actual stringValue?)

             

            Also, what condition is triggering the action method "conditionFruehbucher"? Are you getting this at the end of text editing, or when a menu item is chosen?

            • Re: How to check value of NSCombox
              ak1982 Level 1 Level 1 (0 points)

              Hi, I just Want to know wether The Combobox Says yes Or no. I Think, i use the wrong way of if condition with combobox. How is the correct way? In combobox i have two items "ja" for yes and "nein" for no. I just Check wether the Price get a sale

                • Re: How to check value of NSCombox
                  QuinceyMorris Level 6 Level 6 (2,465 points)

                  A combo box is a kind of text field, not a kind of menu.

                   

                  If you want the user to choose between "Ja" and "Nein", then use a popup button (NSPopupButton) in "pullsDown" mode. See:

                   

                       developer.apple.com/library/content/documentation/UserExperience/Conceptual/OSXHIGuidelines/ControlswithMenus.html

                   

                  under the heading "Command Pop-Down Menu". Or use two buttons.

                • Re: How to check value of NSCombox
                  ak1982 Level 1 Level 1 (0 points)

                  Thanks for your help. I am a Bit of a beginner. Sorry. Can you please help ne how to Check wether the popup Item is item 1 (Ja). With Using if condition. In the Moment i so Not unterstand the Apple library and i Need some help. Thanks a lot.

                    • Re: How to check value of NSCombox
                      eskimo Apple Staff Apple Staff (6,075 points)

                      A popup button’s selected state is returned by its indexOfSelectedItem property.  You can also get the selected item itself via the selectedItem property.

                      Share and Enjoy

                      Quinn “The Eskimo!”
                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                      let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: How to check value of NSCombox
                      ak1982 Level 1 Level 1 (0 points)

                      Hello,

                       

                      so now I changed it to NSPopUpButton, but i still get the error: unexpectedly found nil while unwrapping an Optional value



                      Seems like the if condition doesn't get the value. Here my code:

                       

                       

                          @IBAction func conditionFruehbucher(sender:NSPopUpButton){

                              if fruehbucher.selectedItem == "Ja" {

                                  var fruehbucherBetrag = ((Float(MesseA.integerValue) * MesseAPreis!)+(Float(MesseB.integerValue) * MesseBPreis!)+(Float(MesseC.integerValue) * MesseCPreis!) + (Float(MesseD.integerValue) * MesseDPreis!)+(Float(MesseE.integerValue) * MesseEPreis!))

                                  var fruehbucherBetragFinal = (fruehbucherBetrag * 0,03)

                              }

                           

                          }

                       

                       

                       

                      And here the full code:

                       

                      /

                      /

                      /

                      /

                      /

                      /

                      /

                      import Cocoa

                      class SecondViewController: NSViewController {

                          /

                        

                          @IBOutlet weak var MesseA: NSTextField!

                          @IBOutlet weak var MesseB: NSTextField!

                          @IBOutlet weak var MesseC: NSTextField!

                          @IBOutlet weak var MesseD: NSTextField!

                          @IBOutlet weak var MesseE: NSTextField!

                          @IBOutlet weak var MesseP: NSTextField!

                          @IBOutlet weak var Hochschule: NSButton!

                          @IBOutlet weak var staatlicheBeruflicheSchule: NSButton!

                          @IBOutlet weak var ohneOctanorm: NSButton!

                          @IBOutlet weak var niedrigParentum: NSButton!

                          @IBOutlet weak var hochParentum: NSButton!

                          @IBOutlet weak var fruehbucher: NSPopUpButton!

                          @IBOutlet weak var kennlern: NSComboBox!

                        

                          /

                        

                          var Messeanzahl:Int?

                          var MesseAPreis:Float? = 2615.00

                          var MesseBPreis:Float? = 2715.00

                          var MesseCPreis:Float? = 2815.00

                          var MesseDPreis:Float? = 1895.00

                          var MesseEPreis:Float? = 1395.00

                          var MessePPreis:Float? = 595.00

                          var fruehbucherBetragFinal:Float?

                        

                        

                          /

                        

                          @IBAction func conditionHochschule(sender:NSButton){

                              if Hochschule.state == 1 {

                              MesseAPreis = 1095.00

                              MesseBPreis = 1095.00

                              MesseCPreis = 1095.00

                              MesseDPreis = 1095.00

                              }

                          }

                        

                          /

                        

                          @IBAction func conditionEintaegig(sender:NSButton){

                              if  Hochschule.state == 1 && ohneOctanorm.state == 1 {

                              MesseEPreis = 695.00

                              }

                              else if Hochschule.state == 1 && ohneOctanorm.state == 0 {

                              MesseEPreis = 810.00

                              }

                              else if Hochschule.state == 0 && ohneOctanorm.state == 0 {

                              MesseEPreis = 995.00

                              }

                          }

                        

                          /

                        

                          @IBAction func conditionParentum(sender:NSButton){

                              if  niedrigParentum.state == 1 && hochParentum.state == 0{

                              MessePPreis = 495.00

                              } else if hochParentum.state == 1 && niedrigParentum.state == 0{

                              MessePPreis = 745.00

                              } else if hochParentum.state == 1 && niedrigParentum.state == 1{

                              MessePPreis = 645.00

                              }

                          }

                        

                          /

                        

                          @IBAction func conditionStatlicheEintaegigUndParentum(sender:NSButton){

                            

                              if  staatlicheBeruflicheSchule.state == 1 {

                                  MesseAPreis = 595.00

                                  MesseBPreis = 595.00

                                  MesseCPreis = 595.00

                                  MesseDPreis = 595.00

                                  MesseEPreis = 395.00

                                  MessePPreis = 195.00

                              }

                          }

                        

                          /

                        

                          @IBAction func conditionFruehbucher(sender:NSPopUpButton){

                              if fruehbucher.selectedItem == "Ja" {

                                  var fruehbucherBetrag = ((Float(MesseA.integerValue) * MesseAPreis!)+(Float(MesseB.integerValue) * MesseBPreis!)+(Float(MesseC.integerValue) * MesseCPreis!) + (Float(MesseD.integerValue) * MesseDPreis!)+(Float(MesseE.integerValue) * MesseEPreis!))

                                  var fruehbucherBetragFinal = (fruehbucherBetrag * 0,03)

                              }

                            

                          }

                        

                          override func viewDidLoad() {

                              super.viewDidLoad()

                          }

                        

                          override var representedObject: AnyObject? {

                              didSet {

                                  /

                              }

                          }

                        

                        

                        

                          /

                        

                          override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?)    {

                       

                              /

                            

                              var Messeanzahl = MesseA.integerValue + MesseB.integerValue + MesseC.integerValue + MesseE.integerValue

                              var parentumAnzahl = MesseP.integerValue

                            

                            

                              /

                              if let dest = segue.destinationController as? FourthViewController {

                                

                                  /

                                

                                  dest.AMesse = MesseA.integerValue

                                  dest.AMessePreis = MesseAPreis!

                                

                                  dest.BMesse = MesseB.integerValue

                                  dest.BMessePreis = MesseBPreis!

                                

                                  dest.CMesse = MesseC.integerValue

                                  dest.CMessePreis = MesseCPreis!

                                

                                  dest.DMesse = MesseD.integerValue

                                  dest.DMessePreis = MesseDPreis!

                                

                                  dest.EMesse = MesseE.integerValue

                                  dest.EMessePreis = MesseEPreis!

                                

                                  dest.FinalfruehbucherBetrag = fruehbucherBetragFinal!

                                

                                  dest.PMesse = MesseP.integerValue

                                  dest.PMessePreis = MessePPreis!

                              }

                          }

                        

                      }

                      • Re: How to check value of NSCombox
                        ak1982 Level 1 Level 1 (0 points)

                        I think, now i know where the problem is. The function maybe works but it doesn't send the final number to fruebucherBetragFinal because prepare for segue is allready through, right? How can I handle that?

                        • Re: How to check value of NSCombox
                          ak1982 Level 1 Level 1 (0 points)

                          SOLUTION :-D

                           

                             dest.FinalfruehbucherBetrag = fruehbucherBetragFinal!

                           

                          must be:

                             dest.FinalfruehbucherBetrag = fruehbucher.indexOfSelectedItem