New programmer w/problem

Apprentice
Posts: 11
Joined: 2005.03
Post: #1
I'm new to programming with Cocoa and Xcode, (new being something like three months), and am having some problems with a project I am trying to build. Any help from experienced programmers out there would be most appreciated! I know these questions might be common or simple to fix or whatever else, but please remember I'm new to this sort of thing. (Don't laugh at me 'cause I'm new...)

Alright, question. For some reason, an NSPopUpButton on a puzzle window is returning a null string when I ask it this: [currentShapeType titleOfSelectedItem], where currentShapeType is the name of my button. I can't figure out why this is happening, and if anyone can give me advice that would be nice.

I will tell you what I did to find out that it is returning null: I have an NSObject subclass called PuzzleController, which has outlets to things like NSPopUpButtons in a nib file, which is where I instantiated the class PuzzleController. It has three major actions, and all three of these are triggered by NSPopUpButtons. One of these buttons, who happens to be contained in a hidden box, has its target set to the faulty action in my PuzzleController class: setCurrentShapeType: . This sets the currentType instance string to the string that is SUPPOSED to be returned by [currentShapeType titleOfSelectedItem]. Right after I call the method which sets [currentShapeType titleOfSelectedItem] to currentType, however, the currentType string returns null, which I confirmed by using the NSLog command. I know there can't be a problem with the assignment method because it works when I use something like [shape4 setCurrentType:@"Big shape"], so it must be the [currentShapeType titleOfSelectedItem] method, which leads to my first question: why is it returning null?

I'm sorry if this is too long and difficult to understand. If any of you would like the project to look at and help out I could possibly get it to you. Any help on this issue would be appreciated greatly!
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
for popup buttons ive had better luck returning the index of the selected item then running it through a switch or if statement. Im sorry but you kind of loss me in that second part, its been a pretty long day. Hopefully this will help you out though.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #3
Hm... that could work, but it would be terrible code to maintian: when I added a shape type, I would have to add a new if statement. Plus I'd have to keep track of which shape types are associated with which indexes and... AARRRRGGHHHHHHH!!!!! Ahem. I'd rather use the titleOfSelectedItem method, if it's all right with you. Rasp Sorry. Get some sleep, if you've had a really long day. But thanks for replying, and I'll check out your suggestion...

zzzzzZZZZZzzzzzz ZZZ zzzzzzZZZZZZZZzzzzzzzz
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #4
Ive created a very simple program that returns the title of a popup button, You can examine it and see what you are doing wrong, If you email me krichter@dragon-forged.com I can send you the files.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #5
Hm... *looks over file you sent* THAT seems to work, but mine doesn't. What is different... *goes and looks for differences* Welll... I found that my outlet is set specifically to an NSPopUpButton rather than id. I set my outlet to id and removed the asterisk before the outlet name, but it still doesn't work. I also tried registering the [currentShapeType itemTitleAtIndex:[currentShapeType indexOfSelectedItem]], just to see if that would work, and it didn't either. There's not that many other differences between the two classes... except fot the overabundance of extra code and outlets and such that I have in mine. LOL I have this sneaking suspicion that my outlet, for some reason, isn't connecting during the nib file load. There IS a lot of other stuff in there that could possibly prevent this... in some way...

Solid question: assuming currentShapeType is still an outlet, and I type if (currentShapeType) { other code here... };, would that return true if the outlet is set and false if it isn't? 'Cause I tried that in my code before I ran an NSLog(), and the NSLog() didn't show up, so either it's not connected, or I have the whole thing backwards. Likely the second...

I'll figure it out eventually. I hope. Annoyed If you have anything else to say with what limited information I have given you, please do. And... thought gone. Nevermind. It's too late, and I'm on here again. Rolleyes I will be getting into bed...

ZZZ

P.S. Happy Ressurection Day, to all who read this!

"Far too many to count. I lost track at seven." ~My brother
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #6
Another interesting thing I found out about my program: it runs the faulty action method without my consent, right as the nib file is being loaded. And it has nothing to do with one of the buttons in the window being triggered or anything like that. I disconnected all the buttons that were pointing to that action, and it STILL ran. Weird. If anyone knows why this would happen, I'd like to know too.

Just one of those interesting details...

"Far too many to count. I lost track at seven." ~My brother
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #7
SWEET!!! I got it to work. Just 'cause you might want to know, I'll tell you how:

It's all in the name. The NSPopUpButton type outlet is called currentShapeType, and the action method I was calling is called setCurrentShapeType. For some reason which I still can't figure out, when the outlet was set, the action ran, or something like that. I still can't figure out how messed up it was. It probably ran the method and created an error of sorts, corrupting the outlet. Or something else. I don't know much about this still...

Anyway, all I did was change the method name to setCurrentType, and it worked! Blink *sigh* Just confused, really, but happy that it works... Rolleyes

Thank you for helping me out. I really appreciate it.

Closing thread...

"Far too many to count. I lost track at seven." ~My brother
Quote this message in a reply
Moderator
Posts: 691
Joined: 2002.04
Post: #8
... that you please don't close threads – it stops anybody from following up the thread with more info – and you yourself admitted you weren't entirely sure of the cause of the problem...

Mark Bishop
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #9
Oops. Sorry. Blush I, uh, am kinda new to this. (I signed up yesterday.) Ya. Sorry. Thread is open again. *goes and hides*

"Far too many to count. I lost track at seven." ~My brother
Quote this message in a reply
suhbataar
Unregistered
 
Post: #10
My guess is the reason your setCurrentShapeType method was called is because of a technique known as key-value coding. When the nib file is loaded, it tries to set the outlet, which is really just an instance variable in your class. It does this by calling the "set"-method for this instance variable, which it assumes is setCurrentShapeType. This assumption is based on key-value coding. The key-value coding technique is like an informal agreement that the get-accessor for an instance variable is simply the name of variable itself, and the set-accessor is setName. For example if i have an instance variable myVar (int) i would use the following methods to access it:
Code:
-(int)myVar;
-(void)setMyVar:(int)newInt;
You should always adhere to these rules, since key-value coding (KVC) is used often in Cocoa.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #11
If you want to send me your code i can look over it real quick to see why you were having troubles. And as far as labeling things "id" its a bad idea, i was in a rush when i made that file for you so i didnt take my normal careful steps.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #12
suhbataar Wrote:My guess is the reason your setCurrentShapeType method was called is because of a technique known as key-value coding. When the nib file is loaded, it tries to set the outlet, which is really just an instance variable in your class. It does this by calling the "set"-method for this instance variable, which it assumes is setCurrentShapeType. This assumption is based on key-value coding. The key-value coding technique is like an informal agreement that the get-accessor for an instance variable is simply the name of variable itself, and the set-accessor is setName. For example if i have an instance variable myVar (int) i would use the following methods to access it:
Code:
-(int)myVar;
-(void)setMyVar:(int)newInt;
You should always adhere to these rules, since key-value coding (KVC) is used often in Cocoa.
Not to disturb, but loading nibs works even if you don't implement accessors to said instance variables.
Quote this message in a reply
suhbataar
Unregistered
 
Post: #13
Quote:loading nibs works even if you don't implement accessors to said instance variables
That's a good point. Hmm... I just figured KVC was involved somehow, since trebek's setCurrentShapeType-method was called automatically. I guess, the accessor is called if it is implemented, and otherwise some default method is used for setting the variable.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2005.03
Post: #14
Thank-you everyone for the help! That part of the program works fine now, (There are some other parts I'm still working on), and now I have more knowledge of how This stuff works; thank you so much! If I have any more problems I'll be sure to put them here. Wait, who am I kidding. Of course I'll have problems...

Rasp Gotta go work on it...

"Far too many to count. I lost track at seven." ~My brother
Quote this message in a reply
Post Reply