3

我设置了一个BOOL调用isUsingiPad来检测我的用户何时使用 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;
}
4

2 回答 2

1

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

现在确保您的用户每次都返回到正确的屏幕,这取决于您的设置,您可能希望在viewDidLoad或中初始化它viewDidAppear

 - (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;
}
于 2012-04-03T20:20:03.483 回答
1

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

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

这样做你不需要做任何手动加载或 if 语句。

于 2012-12-10T12:04:35.020 回答