获得活跃的故事板

时间:2012-08-30 19:09:10

标签: ios cocoa-touch

我使用下面的代码来检查用户是否已登录或退出,并且它正常工作。但是因为我按名称获取故事板,我总是将用户发送到iPhone"在"或" out"查看控制器。如果我得到活跃的故事板,它会工作。我该如何修复代码?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    BOOL user = ...;
    NSString *segue = user ? @"in" : @"out";
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
    UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:segue];
    [self.window setRootViewController:viewController];

    return YES;
}

1 个答案:

答案 0 :(得分:1)

如果您要做的就是从不同的故事板中获取viewController,具体取决于您的应用程序是在iPad上运行还是在iPhone上运行,您可以使用if语句执行此操作:

NSString *storyboardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyboardName = @"iPad";
} else {
    storyboardName = @"iPhone";
}
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];

如果您确实需要获得“活动”故事板,无论情况如何,这个先前的答案可能对您有所帮助:

UIStoryboard: What's the Correct Way to Get the Active Storyboard?