Dynamically load nib for iPhone/iPad within view controller

前端 未结 5 2084
悲哀的现实
悲哀的现实 2020-12-24 13:30

I have converted an iPhone application using the wizard like thing in XCode into a universal app.

It builds fine but obviously looks a bit rubbish in some areas :)<

相关标签:
5条回答
  • 2020-12-24 14:04

    You can have your cake and eat it too. Just add a method that wraps initWithNibName:bundle: and adds your myLovelyData parameter:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  myLovelyData:(id)data
    {
        if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
        {
            // Custom initialization using myLovelyData
            //
        }
        return self;
    }
    
    0 讨论(0)
  • 2020-12-24 14:17

    I think it will be better to create C file.
    FileHelper.h

    #import <Foundation/Foundation.h>
    BOOL isIPad();
    NSString *addIPadSuffixWhenOnIPad(NSString *resourceName);
    

    FileHelper.m

    #import "FileHelper.h"
    BOOL isIPad() {
        if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2) {
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
                return YES;
            }
        }
        return NO;
    }
    
    NSString *addIPadSuffixWhenOnIPad(NSString *resourceName) {
        if(isIPad()) {
            return [resourceName stringByAppendingString:@"-iPad"];
        }
        else {
            return resourceName;
        }   
    }
    
    0 讨论(0)
  • 2020-12-24 14:19

    I just put these two methods in a class called IPadHelper, and use the addIPadSuffixWhenOnIPad method to conditionally pick between two nibs depending on platform

    + (BOOL)isIPad{
        if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2){
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
                return YES;
            }
        }
        return NO;
    }
    
    + (NSString *)addIPadSuffixWhenOnIPad:(NSString *)resourceName{
        if([IPadHelper isIPad]){
            return [resourceName stringByAppendingString:@"-iPad"];
        }
        else {
            return resourceName;
        }   
    }
    

    see here http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html for more explanation of the first method...

    0 讨论(0)
  • 2020-12-24 14:22

    EDIT: @Adam's answer below is the correct answer.

    To determine which nib to load, do the following, and scrap your initWithMyLovelyData method and use a property to set the data. You should be able to easily move all your init code into the property setter method.

    MyViewController *viewController;
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        viewController = [[MyViewController alloc] initWithNibName:@"ipadNIB" bundle:nil];
    } else {
        viewController = [[MyViewController alloc] initWithNibName:@"iphoneNIB" bundle:nil];
    }
    
    viewController.myLovelyData = someData;
    
    0 讨论(0)
  • 2020-12-24 14:25

    Actually, Apple does all this automatically, just name your NIB files:

    MyViewController~iphone.xib // iPhone
    MyViewController~ipad.xib // iPad
    

    and load your view controller with the smallest amount of code:

    [[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything
    
    0 讨论(0)
提交回复
热议问题