I have read a lot about making an universal app from iPhone app. But I didn't find anything about my situation : One part of my app is built in Storyboard, but few views are created in .xib-s. How to connect it all and create universal app in it?

有帮助吗?

解决方案

The most common case I think would be to check device type before instantiating the view controller. At that point you can instantiate the appropriate view controller with the appropriate nib. Note that for iPad vs iPhone you may choose to use:

  • different UIViewControllers (an iPad specific one and an iPhone specific one)
  • the same UIViewController but with different nibs.
  • the same UIViewController with the same nib (use resizing or Auto Layout to scale up/down)

You can obviously skip the device check if you use the same view controller with the same nib (since there would be no code difference). Also you wouldn't need to do the device check in areas of the code that won't run on multiple device types. For example code inside an iPad-only view controller can assume iPad and skip the device check. Code you are certain will only run on iPhone can also skip the check and assume iPhone. That is all to say try not to litter your code with checks like this.

- (IBAction) handleButtonPress {

    UIViewController *vc = nil;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        vc = [[MyIPadViewController alloc] initWithNibName:@"MyIPadViewController" bundle:nil];
    } else {
        vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    }

    [self presentViewController:vc animated:YES completion:NULL];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top