开发者

initializing rootViewController from different xibs depending on the device the App runs on

Hey guys, I Have the following Problem: I have an IPad App a开发者_高级运维nd 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜