0

Hey guys, I Have the following Problem: I have an IPad App and now I want it to run on IPhone also. (I know it should be the other way round, but I cant change it). So now i want to decide which view i need by using:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
     // The device is an iPad running iOS 3.2 or later.
}
else {
     // The device is an iPhone or iPod touch.
}

This works well for all views exept the RootViewController.
No Matter where i set the xib for the RootViewController, it always displays the one i defined for the IPad-App.

This is my Code in the App-Delegate:

viewController = [VoycerUniversalAppViewController sharedInstance]; 


UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window addSubview:myNavigationController.view];   
//[self.window addSubview:self.vcSplashScreen.view];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

return YES;  


And this is the Code in my [VoycerUniversalAppViewController sharedInstance]:

+ (VoycerUniversalAppViewController*) sharedInstance 
{
    @synchronized(self) 
    {
        if(sharedInstance == nil) 
        {
            // only allocate here - assignment is done in the alloc method
            if (![AppState sharedInstance].loggedIn) 
            {
                [AppState sharedInstance ].loggedIn = FALSE;            
            }
        }
    }   
    return sharedInstance;
}

+ (id) allocWithZone:(NSZone*)zone {
    @synchronized(self) {
        if(sharedInstance == nil) {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
            {
                // The device is an iPad running iOS 3.2 or later.
                NSLog(@"The device is an iPad running iOS 3.2 or later.");
                sharedInstance = [[super allocWithZone:zone] initWithNibName:@"VoycerUniversalAppViewController" bundle:nil];
            }
            else {
                // The device is an iPhone or iPod touch.
                NSLog(@"The device is an iPhone or iPod touch.");
                sharedInstance = [[super allocWithZone:zone] initWithNibName:@"VoycerUniversalAppIPhoneViewController" bundle:nil];
            }   
            // allocate and assign instance variable on first allocation                    
            return sharedInstance;
        }
    }
    return nil;
}


I looked almost everywhere i think if i missed some setting or something, but i couldnt find any.
Does anyone of you have an idea, why constantly loads the VoycerUnversalAppViewController instead of the other one?
Both Xibs are linked to the same class.
If the solution is a quite simple one and obvious please don't blame me, i'm really new to xcode, IB and objective-c (i code in objective-c for 1 1/2 month now).

Thx in advance.
Maverick1st.

Maverick1st
  • 3,774
  • 2
  • 34
  • 50

1 Answers1

2

For the main NIB, there are settings in the info.plist file: NSMainNibFile and NSMainNibFile~ipad.

In the NIB file you can create the application delegate and set up a navigation controller.

Ciprian L.
  • 587
  • 2
  • 7
  • When i started the Project i made a MainWindow.xib which contains my Delegate and also my ViewController, because a colleague of me told me to do so. If i change the xib file for the appropriate View Controller in the MainWindow.xib it has no effect on the view. It still displays the wrong one. Since i have only one MainWindow.xib for both IPad and IPhone i set both NSMainNibFiles in info.plist to MainWindow. Or should i make a complete new MainWindow.xib for IPhone? But thx for the tips even if they didn't work right now. It was useful info for me. :) – Maverick1st Jan 17 '11 at 18:20
  • Yes, create two NIBs and add them to the info.plist. It should work. – Ciprian L. Jan 17 '11 at 18:27
  • Unfortunately not. I created a IPhone_MainWindow.xib and set it as NSMainNibFile~ipad, but although it steps into else { // The device is an iPhone or iPod touch. NSLog(@"The device is an iPhone or iPod touch."); sharedInstance = [[super allocWithZone:zone] initWithNibName:@"VoycerUniversalAppIPhoneViewController" bundle:nil]; } it wont show my View with the correct Nib. How can i connect my xib for the iphone view with the mainWindow.xib? I can only set the ViewController Class and the delegate, and not the xib. – Maverick1st Jan 18 '11 at 12:21
  • ok, now i got my IPhone_MainWindow.xib to load the correct xib file for my RootViewController. I only forgot to set the correct NibFile for the ViewController in the IPhone_MainWindow.xib. Thx for the help – Maverick1st Jan 18 '11 at 13:05
  • If you mean i should click on the hook, then i did it already :). – Maverick1st Jan 18 '11 at 15:24