InitWithNibName is either called twice, or xib is loaded incorrectly

I am programming a Cocoa application and want the application to work as a kind of wizard. Therefore, in the main window, I have a user view that interacts with the user and changes sign on the device’s activation screen when they go through the steps of the wizard. I am currently overriding the wakeFromNib WizardViewController method:

- (void)awakeFromNib{ //If no redirect request save, add first view: ID Login NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *tokenRequest = [defaults objectForKey:@"redirectRequestToken"]; if (!tokenRequest){ SignInWithIDViewController *signInViewController = [[SignInWithIDViewController alloc] initWithNibName:@"SignInWithIDViewController" bundle:nil]; [wizardView addSubview:[signInViewController view]]; } else { NSLog(@"Have already logged in."); } } 

As is, the initWithNibName in the SignInIDViewController is called twice, once explicitly, and again when the view is loaded (presumably via loadView). However, if I just call init, then the name initWithNib is called only once, but the wrong xib file is loaded (class DeviceActivationViewController). I cannot understand what I'm doing wrong, because signInViewController does not have to be initialized twice, but I need the corresponding xib file in IB to display.

The only other method that I have in this class at this time that is not a user interface. IBAction is the generated initWithNibName method plus the added NSLog statement.

0
cocoa xib nsviewcontroller init
May 31 '12 at 13:51
source share
1 answer

I think creating objects in IB (blue cubes) and creating them in code is a problem. If you created objects for them in IB, then they will be created in awakeFromNib, you also should not call the alloc init command in them, which will create a new instance.

I don't have much experience using view controllers in OSX, but it seems you cannot connect IBActions to the view controller (as the owner of the file). The way I did this was to subclass the custom view (created for you when adding the view controller), change the class of this view to your new subclass, and put the action methods in this class. It seems that this should be something that will be handled by the view controller, but I think it does not work, has something to do with the view controller not being in the responder chain in OSX (whereas I think it’s in iOS).

After editing: after circumventing memory management issues, I think I found a better way to do this. You can and probably should (to fit the Apple MVC paradigm) put the button methods in the view controller class, and not in the view, as I said above. In fact, you can connect IBActions to the view controller (as the owner of the file), you just need to make sure that the view controller is saved when you create it in the code. To do this, you need to make signInViewController a property in any class in which you create the SignInViewController class, and use "save" in the property declaration. Then you do not (and should not) create any of the blue cubes in IB.

+1
Jun 01 '12 at 16:12
source share



All Articles