In my AppDelegate method applicationDidFinishLaunching:
// Initialize here
NSLog(@"Application finished launching...");
MyMain* mainAccess; // To send messages to SAAMain
mainAccess = [[Main alloc] init];
NSLog(@"Setting title...");
version = @"v1.0";
[mainAccess setTitleVersion:version]; // Set Title with proper version
As you can see, I created an instance of my MyMain class in order to send a message to it (where all my important “main” methods are located). In this case, I’m trying to set the title.
My MyMain class has a method setTitleVersion and takes a NSString as a parameter (version).
NSLog(@"setTitleVersion called");
NSString *title = [NSString stringWithFormat:(@"Version: %@", version)];
//[_mainWindow setTitle:([NSString stringWithFormat:title])];
[_mainWindow setTitle:@"Test"];
NSString *mainWindowTitle = _mainWindow.title;
NSString *test2 = _mainWindow.value;
NSLog(@"_mainWindow.title: %@\n_mainWindow.value: %@", mainWindowTitle, test2);
I have an outlet of the main window attached to this MyMain class (as you can see with _mainWindow).
I added some NSLogs to try to debug myself. Both the value and title come back as null.
What am I doing wrong? The method is getting called as I can see in the console output. It’s as if the _mainWindow outlet isn’t working properly.
Suppose your child has a bowl of ice cream, and wants a cherry on top. You go to the freezer and get out the ice cream carton. You get a bowl out of the cabinet. You put some ice cream in the bowl. Now you have a bowl of ice cream. You get a cherry out of the refrigerator and put a cherry on it. Then you throw the bowl of ice cream (with cherry on top) away. Now you’re confused, because your child’s bowl of ice cream still has no cherry on top! What did you do wrong?
This scenario is a metaphor for your problem. You need to send the
setTitleVersion:message to your existing instance ofMyMain. You can’t just create an entirely new instance ofMyMain, send the message to it, and then throw away that new instance.Your comment on
rdelmar‘s answer says that your existing instance ofMyMainis in your main nib, and you have an outlet to it. I assume that means you have an outlet of typeMyMainon yourMyAppDelegate, and that the outlet is hooked up to aMyMainobject in your nib. In that case, you want to send thesetTitleVersion:message to the object in the outlet. If your outlet were namedmainObject, you would do it like this:If you haven’t created an outlet in
MyAppDelegateyet, you can create it and hook it up in one step using Xcode’s assistant editor. Open the assistant editor and make sure it is showingMyAppDelegate.h. Then control-drag from theMyMainobject in the nib into your@interface MyAppDelegate, like this:If you already have an outlet declared in your
MyAppDelegate, you can connect it in the nib editor. Control-click on theMy App Delegateplaceholder in the nib’s Objects list. Then, in the outlets pop-up, drag from the outlet’s circle to theMyMainobject placeholder, like this: