MainWindow.xib for ipad and iphone

点点圈 提交于 2020-01-14 05:47:08

问题


I am trying to build on this demo which contains both a MainWindow.xib and a ImageManipViewController.xib . But my app contains the following code in the AppDelegate.m and my app does not (yet?) contain a MainWindow.xib. Is it better/best for me to add 2 MainWindow.xib files (one for iPhone and one for iPad) too (that are called in my method, each calling its own xib file) or is are the MainWindow.xibs just extra?

(Btw, if it turns out that the answer is that the MainWindow.xibs are redundant, then can you say why it might have been used in the original demo? Was it likely just a result of the author simplifying the steps in creating the demo, for example?) I have not found a way to contact the author directly.

If there is a better approach to developing a "universal" app, please advise me.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;

回答1:


You can build your main window and its attached subviews (and even view controllers) either programmatically or by using a Xib file. The result is the same, but small demo projects often use Xib files in order to simplify the code and focus on the part that they want to show you.

Two Xibs, one for iPad and one for iPhone is perfectly fine because iPad apps are encouraged to use views that make the most of the iPad large screen; for this reason, keeping them as separate files is recommendable. Your model and controllers should be common to iPad and iPhone, though.

And about loading Xibs, if your project is targeting iOS 4.0 or newer, you can name your xib files "BSViewController~iphone.xib" and "BSViewController~ipad.xib" and avoid some conditionals in your code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
}


来源:https://stackoverflow.com/questions/14341605/mainwindow-xib-for-ipad-and-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!