2

I have an universal application that had all views in the same file MainWindow.xib. Today I decided to separate these views into their respective xib files (to have MainMenuController.h, MainMenuController.m and MainMenuController.xib for example). Now I can't receive and IBActions. Here is what I did step by step:

  1. I created a new .xib file named MainMenuController.xib and set it's File's Owner to the already existing MainMenuController class.

  2. I copied the view corresponding to the MainMenuController from the MainWindow.xib file and pasted it into the newly created MainMenuController.xib. I set the File's Owner's view to be the newly pasted view (connected in IB).

  3. In the info.plist, I removed the entries for the "Main xib file base name", so the app doesn't open with MainWindow.xib automatically.

  4. I modified the app delegate to create the window programatically and add the MainMenuController to it with this code:

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    mainMenuController = [[MainMenuController alloc] init];
    self.window.rootViewController = mainMenuController;
    [self.window makeKeyAndVisible];
    [mainMenuController release];
    

    "mainMenuController" and "window" are instance variables and also declared as properties.

  5. I have only one AppDelegate class and main.m contains:

    int main(int argc, char *argv[])
    {
      @autoreleasepool {
      return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }}
    

Now when the application opens, I see the MainMenuController's view come up. At this point I began to wire the IBOutlets and IBActions in the pasted view in the new xib file. Although I see the IBOutlets in the File's Owner and connect them properly, the function for the IBAction is never called when I press the button.

Possible errors that came to my mind: (1) application window not properly set, it doesn't pass events, (2) something is stuck or lost while copying the view, still pointing to the old owner (3) something wrong stuck in xcode project

What do you think the problem could be? Is it one of the above? How can I solve this? Any help is appreciated.

Thanks in advance.

Murat Ögat
  • 1,342
  • 2
  • 14
  • 24

1 Answers1

0

Your creation of the main view controller:

mainMenuController = [[MainMenuController alloc] init];

makes no reference to the XIB. So you're doing a purely programmatic creation with no reference to whatever may or may not be in the XIB. Hence your view controller appears to work but none of the outlets or actions are wired up. You may be making reference elsewhere, but I guess not appropriately.

Probably you want:

mainMenuController = [[MainMenuController alloc] 
                              initWithNibName:@"MainMenuController" bundle:nil];

That'll explicitly use whatever is in the XIB to create the controller.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • Thanks for the answer, but I tried both init and initWithNibName. It makes no difference. – Murat Ögat Sep 06 '12 at 21:38
  • It's impossible for it to make no difference. If you do a `UINib nibWithNibName:bundle:` then do you get a valid object back? – Tommy Sep 06 '12 at 21:40
  • This function never returned nil for some reason, even for garbage strings. After struggling with this problem through the night I decided that something deep is wrong with the environment and started from scratch. Thank you very much for trying to help anyway. – Murat Ögat Sep 07 '12 at 06:48