简体   繁体   English

在iOS上制作通用应用

[英]Making an universal app on iOS

I have read a lot about making an universal app from iPhone app. 我已经阅读了很多有关从iPhone应用程序制作通用应用程序的信息。 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. 但是我没有发现任何有关我的情况的信息:我的应用程序的一部分是在Storyboard中构建的,但是很少在.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: 请注意,对于iPad和iPhone,您可以选择使用:

  • different UIViewControllers (an iPad specific one and an iPhone specific one) 不同的UIViewControllers(iPad专用和iPhone专用)
  • the same UIViewController but with different nibs. 相同的UIViewController,但笔尖不同。
  • the same UIViewController with the same nib (use resizing or Auto Layout to scale up/down) 具有相同笔尖的相同UIViewController(使用调整大小或自动布局来放大/缩小)

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. 例如,仅iPad的视图控制器中的代码可以使用iPad并跳过设备检查。 Code you are certain will only run on iPhone can also skip the check and assume iPhone. 您确定只能在iPhone上运行的代码也可以跳过检查并假设使用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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM