Pregunta

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.

¿Fue útil?

Solución

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 of MyMain. You can't just create an entirely new instance of MyMain, send the message to it, and then throw away that new instance.

Your comment on rdelmar's answer says that your existing instance of MyMain is in your main nib, and you have an outlet to it. I assume that means you have an outlet of type MyMain on your MyAppDelegate, and that the outlet is hooked up to a MyMain object in your nib. In that case, you want to send the setTitleVersion: message to the object in the outlet. If your outlet were named mainObject, you would do it like this:

[self.mainObject setTitleVersion:version];

If you haven't created an outlet in MyAppDelegate yet, 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 showing MyAppDelegate.h. Then control-drag from the MyMain object in the nib into your @interface MyAppDelegate, like this:

create and connect new outlet

If you already have an outlet declared in your MyAppDelegate, you can connect it in the nib editor. Control-click on the My App Delegate placeholder in the nib's Objects list. Then, in the outlets pop-up, drag from the outlet's circle to the MyMain object placeholder, like this:

connect existing outlet

Otros consejos

You say you have an IBOutlet to connected to the Main class, but I'm a little confused as to what is what. First, you don't connect outlets to classes, you connect them to objects. It's hard to tell from your question, but I suspect the problem is that you're creating one instance of Main, mainAccess, in code, but that's not the instance that you have an outlet to in IB.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top