【问题标题】:Making an iPad nib load in a universal app在通用应用程序中加载 iPad 笔尖
【发布时间】:2012-04-17 13:02:08
【问题描述】:

我设置了一个名为isUsingiPadBOOL 来检测我的用户何时使用iPad。我用这个来做到这一点:

UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    isUsingiPad = YES;
}

当我的应用程序首次启动时,它会检查正在使用的设备是否已通过我的注册。如果有,那么它将用户发送到我的应用程序的主视图控制器。但是......当注册用户(使用 iPad)注册、关闭应用程序,然后重新打开它时,它们会被发送到 iPhone 笔尖而不是 iPad。我的应用程序中的每个视图都有 2 个笔尖。一款用于 iPhone,一款用于 iPad。有一个 View Controller 控制每组 2 个。我已经放置了代码来处理它是 iPhone 还是 iPad。我的问题是:我应该添加什么来确保用户每次都能使用 iPad 笔尖?我在哪里添加这个?我可以编辑这个问题以包含任何必要的代码。提前致谢。

编辑:更新-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: 方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    isUsingiPad = YES;
}

if (!isUsingiPad) {
    self.viewController= [[PassportAmericaViewController alloc] initWithNibName:@"PassportAmericaViewController" bundle:nil];
} else {
    self.viewController = [[PassportAmericaViewController alloc] initWithNibName:@"PassportAmericaViewController-iPad" bundle:nil];
}

self.window.rootViewController = self.viewController;

[self.window addSubview:navigationController.view];

[self.window makeKeyAndVisible];

return YES;
}

【问题讨论】:

    标签: ios ipad xib universal


    【解决方案1】:

    这是 Apple 在应用程序模板中用来实现这一点的方法,它在您的 AppDelegates applicationDidFinishLaunchingWithOptions 中实现:

    现在确保您的用户每次都返回到正确的屏幕,根据您的设置,您可能希望在 viewDidLoadviewDidAppear 中初始化它。

     - (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 = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    【讨论】:

    • 谢谢。我会在几分钟后添加它并标记答案,以便您获得代表。
    • 我有点困惑。我应该用什么来替换“self.viewController”中的“viewController”?我收到一条错误消息,提示在对象上找不到 viewControler。
    • 你在哪里实现这个,在你的视图控制器实现文件中,还是在你的 AppDelegate 中?
    • 先在 AppDelegate 中试用。
    • 好吧,请稍等一下,我正在使用手机,所以我的资源非常有限...您是在导入视图控制器文件吗?
    【解决方案2】:

    为了在通用应用程序中为 iPad/iPhone 动态加载 nib,您应该使用以下命名约定:-

    • iPhone - MyNibName.xib
    • iPad - MyNibName~ipad.xib

    这样做,您不需要执行任何手动加载或 if 语句。

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2016-11-30
      • 2012-04-02
      相关资源
      最近更新 更多