NSPopUp Selection Problem

Back Ground:

// incomeMonOne = NSPopUp containing months Jan --> Dec

// incomeMonTwo = NSTextField

// index of incomeMonOne = 3 for Mar


Question:

How do I get the title of (index + 6)?

I want to copy this title to incomeMonTwo.

example: title of (3 + 6) = Sep

Replies

Doesn't this work ?


              let newIndex = incomeMonOne.indexOfSelectedItem + 6 
              let titleOfOtherItem =  itemTitle(at: newIndex)
              incomeMonTwo.stringValue = titleOfOtherItem     // If it is OSX App ; if IOS, that would be .text   


Just check wether index start at 0 or 1


Note: You had a similar question a few days ago. Don't forget to close them by marking correct answer.

Claude, this doesn't work for me. Here is a copy of my code:


if incomeMonOne.indexOfSelectedItem > 6 {

if dialogOK_Cancel(warning: "Income Month One", reason: "month must be < Jul") {

dialogForceCancel(reason: "User Cancelled")

exit(0) }

let newIndex = incomeMonOne.indexOfSelectedItem + 6

let titleOfOtherItem = incomeMonOneTitle(at: newIndex) ERROR HERE: Use of unresolved identifier 'incomeMonOneTitle'

incomeMonTwo.stringValue = titleOfOtherItem

}

}

I've cleaned the app, and the error comes back when I try to compile it.

That's not what I suggested.


You should use:

let titleOfOtherItem =  incomeMonOne.itemTitle(at: newIndex)     // Sorry, here I forgot  incomeMonOne.

and not

let titleOfOtherItem =  incomeMonOneTitle(at: newIndex)


So the code becomes:

              let newIndex = incomeMonOne.indexOfSelectedItem + 6
              let titleOfOtherItem =  incomeMonOne.itemTitle(at: newIndex)  
              incomeMonTwo.stringValue = titleOfOtherItem     // If it is OSX App ; if IOS, that would be .text


I see another error in your code:

if incomeMonOne.indexOfSelectedItem > 6 {
            if dialogOK_Cancel(warning: "Income Month One", reason: "month must be < Jul") {
                dialogForceCancel(reason: "User Cancelled")
                exit(0) 
          }
        let newIndex = incomeMonOne.indexOfSelectedItem + 6
        let titleOfOtherItem =  incomeMonOneTitle(at: newIndex)   // ERROR HERE: Use of unresolved identifier 'incomeMonOneTitle'
        incomeMonTwo.stringValue = titleOfOtherItem
     }
 }

Seem it misses a closing bracket between lines 5 and 6; otherwise, when

incomeMonOne.indexOfSelectedItem <= 6

you skip directly at line 11.

Claude, thanks for pointing out my error! I have changed the code as follows, but nothing shows in NSTextBox/incomeMonTwo when executed.

Why?


@IBAction func monthOneAction(_ sender: NSPopUpButton) {

if incomeMonOne.indexOfSelectedItem > 6 {

if !dialogOK_Cancel(warning: "Income Month", reason: "Income Month must be in range of Jan thru Jun") {

dialogForceCancel(reason: "Income Month One Error")

exit(0)

}

else {

let newIndex = incomeMonOne.indexOfSelectedItem + 6

let titleOfOtherItem = incomeMonOne.itemTitle(at: newIndex)

incomeMonTwo.stringValue = titleOfOtherItem

}

}

}

In the following :


@IBAction func monthOneAction(_ sender: NSPopUpButton) {
        if incomeMonOne.indexOfSelectedItem > 6 {
           if !dialogOK_Cancel(warning: "Income Month", reason: "Income Month must be in range of Jan thru Jun") {
                dialogForceCancel(reason: "Income Month One Error")
                exit(0)
            }
        else {
              let newIndex = incomeMonOne.indexOfSelectedItem + 6
                let titleOfOtherItem =  incomeMonOne.itemTitle(at: newIndex)
                incomeMonTwo.stringValue = titleOfOtherItem
            }
        }
    }


the else at line 7 corresponds to the if !dialog.

So, lines 7 to 11 is within if incomeMonOne.indexOfSelectedItem > 6 {

So, those lines not be exexuted if index <= 6


EDITED May 20

Did it solve the issue ?