将“initWithNibName”更改为“storyboardWithName”

时间:2013-04-09 17:03:48

标签: ios xcode uistoryboard nib

我在应用程序启动期间按照一些教程创建了一个打开的门动画,但它正在调用

xib文件,我想调用故事板,我没有足够的经验。 这是我的代码

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

2 个答案:

答案 0 :(得分:5)

如果您只想在应用启动时加载故事板的初始视图控制器,只需在YES中返回application:didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

如果要从故事板加载特定控制器,则需要先通过

获取故事栏实例
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];

然后用它来实例化你需要的控制器

UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];

其中controllerIdentifier已作为故事板标识符分配给Interface Builder中的控制器。

这是一个加载特定视图控制器的示例,在启动时显示它。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
    UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];
    self.window.rootViewController = controller;
    return YES;
}

答案 1 :(得分:1)

如果您启动新的iOS项目并选择“使用故事板”,则会自动为您预装故事板。

故事板是一个包含应用程序所有控制器(场景)的地方,要引用一个,你需要使用

    UIViewController *controller = [[UIStoryboard storyboardWithName:@"storyboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"an identifier"];