简体   繁体   中英

How to develop an universal app for iPhone + iPad without xib files?

I want to make an universal build, but I create my UI entirely programmatically.

1) Can I add my different view controller classes to the device-specific resources directories? Or must classes be shared along both devices?

2) If I must share the classes along both devices, what's the safest way to conditionally load classA or classB depending on if it's an iPhone or iPad device?

1) You can not add the view controller classes in two places, because at compile time the same symbol (ie class name) will be found in two places and the app will not link.

2) I would say that the safest way would be to have different .xib , but if you want to meke it work just from code, you could create a singleton view manager that only does the correct class loading. Then, in the rest of your code, all you need to do is: [MyViewManager sharedInstance] instantiateViewController:kMyDetailsView] and display the view as you desire.

For determining the current device you can use a statement like this: #define iPad [UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad . It can be included in your prefix file making it available to the whole project.

On every view controller you can do a device check to see which device is running your app and accordingly build your UI. ie, use only one set of view controllers and in the *.m files, in an if-else loop, build your UI accordingly.

Refer to this post to do device checks: Determine device (iPhone, iPod Touch) with iPhone SDK

PS: In your if-else, do an if(iPad){} else{} check as a best practice, since, often times than not, people forget to check for iPod Touch if they do if(iPhone){}else{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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