Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

krmarien

macrumors newbie
Original poster
Jan 8, 2010
6
0
Hello,

I'm new to Obj-C. But I'm trying to create my first app.
It is a small app for the statusmenu.
In the Interface Builder I have created the menu with nsmenuitems.

But now I want to access some nsmenuitems in my code. So I created an IBOutlet:
Code:
IBOutlet NSMenu *menu;
And I connected this in the IB with the nsmenu.

But when I want to access an menuitem I receive null:
Code:
NSLog(@"title: %@", menu);

Can somebody explain me what is wrong.

Thanks,
Kristof
 
Where is the NSLog() in your application? If it is in the -init method of your principle object, it is very likely that the menu object has not been created yet. Try putting the log statement in -awakeFromNib
 
The NSLog was in the applicationDidFinishLaunching. I have put it now in the awakeFromNib. But it doesn't change, the output remains null.

[EDIT]
Maybe it's easier if you can see the project. I have added it.
[/EDIT]
 

Attachments

  • KotnetAutologin.zip
    228.8 KB · Views: 66
I am probably doing something wrong (other than trying to build using 10.5), but this line
Code:
NSString *user = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"userName"];

is returning a nil value for user. Are you sure that key is correct? I cannot even locate that key or any similar key on my system (tried several variations using defaults read)

Note that sometimes methods will fail silently (upstream of where you are having a problem) and you may be on your own to try to figure out where the failure is.
 
The value for the user isn't nil on my system. But that can't be the cause for failing in accessing the menu.

But I tried to access the menu in the StatusMenu class. it works in the awakeFromNib function but not in a function I call later (function not added in the zip).
Can somebody explain this?

Kristof
 
In your applicationDidFinishLaunching: method, you have

Code:
[[ConnectToKotnet alloc] connect]

This is creating a new instance of that object, which will have its instance variables set to nil. Creating an object like this does not cause it to acquire the properties of the object you instantiated in the nib file, it creates a new, blank object. In other words, you need to provide an outlet to the nib-instantiated object to your app delegate so that it can access the object that the nib instantiated, which is where the IBOutlets are valid. Make sense?

EDIT: Actually, you could just put the awakeFromNib method in the ConnectToKotnet object itself, which would consist of
Code:
-(void)awakeFromNib {
[self connect];
}

Also, your project is not using Garbage Collection, so the -alloc method you used above creates a leaked object. If you do not use garbage collection, you should always have a pointer to every object you create with -alloc, -new, or -copy so that you can dispose of them properly.
 
Thanks for your help.

The problem were the pointers. I created an instance of StatusItem. And later on I created it again. But the IBOutlet to the menu was gone.
I have rewriten the code. And now it works.

Due Sydde comment I have found it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.