开发者

How can i add an iphone interface to a ipad app so that the proper interface is selected when the app is started?

开发者 https://www.devze.com 2023-03-25 12:51 出处:网络
I have an ipad app and would like to make it run also on iphone (in the same 开发者_如何学JAVAapp) so when i install the app on an iphone/ipad, the proper view is selected.

I have an ipad app and would like to make it run also on iphone (in the same 开发者_如何学JAVAapp) so when i install the app on an iphone/ipad, the proper view is selected.

Honestly i don't know where to begin, could you give me some ideas of what i am dealing with?


If you look at the default project for a Universal Application, you can see how this works (see here the applicationDidFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Detects if it is an iPhone.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        // It's an iPhone
        self.viewController = [[Test123ViewController alloc] initWithNibName:@"Test123ViewController_iPhone" bundle:nil]; 
    } else {
        // It's an iPad
        self.viewController = [[Test123ViewController alloc] initWithNibName:@"Test123ViewController_iPad" bundle:nil]; 
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

This allows you to select the correct nib for your application's view controller based on the device.


Content_iPhone,Content_iPad are same views logic but different nibs.

so u can get load them like this

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        // load the content controller object for Phone-based devices
        [[NSBundle mainBundle] loadNibNamed:@"Content_iPhone" owner:self options:nil];
    }
    else
    {
        // load the content controller object for Pad-based devices
        [[NSBundle mainBundle] loadNibNamed:@"Content_iPad" owner:self options:nil];
    }

    [self.window addSubview:self.contentController.view];
    [window makeKeyAndVisible];

}
0

精彩评论

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