一起使用笔尖和故事板

时间:2013-12-08 20:57:53

标签: ios storyboard uitabbarcontroller xib nib

根据我在互联网上看到的内容,笔尖和故事板在同一个项目中相互比较。现在我正在创建一个标签式应用程序,并希望执行以下操作:

标签1:用笔尖构建,

标签2:用故事板构建,

选项卡3:使用笔尖构建

最初我用nib创建了所有东西,所以在我的委托中,我用这个代码从一个标签转到下一个标签:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil] autorelease];
    } else {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil] autorelease];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPad" bundle:nil] autorelease];

    }
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;



}

我没有SecondViewController_ .xib,而是需要使用SecondViewController _ .storyboard。我该怎么做呢?我可以将名称“SecondViewController_ .nib”更改为“SecondViewController _ .storyboard”吗?我认为这不会起作用..

任何帮助都将非常感谢!!

编辑: 我正在使用的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Events", @"Events");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self; }

2 个答案:

答案 0 :(得分:2)

您可以通过创建故事板然后在其中创建SecondViewController场景来执行您的建议。在代码中,您可以通过调用initWithNibName:storyboardWithName:来替换instantiateViewControllerWithIdentifier:

然而,由于故事板中的一个主要功能是能够将多个控制器连接在一起并定义它们之间的关系,因此很难想象为什么为一个控制器执行此操作会是一个好主意。

答案 1 :(得分:1)

菲利普说,只需使用instantiateViewControllerWithIdentifier

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3;
    UIStoryboard *storyboard;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        storyboard      = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
        viewController2 = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil] autorelease];
    } else {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil] autorelease];
        storyboard      = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
        viewController2 = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPad" bundle:nil] autorelease];

    }
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

这假设您的故事板被称为iPhone.storyboardiPad.storyboard,但更改上述代码以匹配您对这两个故事板的称呼。此外,这再次假定您定义了该场景以承载Second的“故事板ID”(您通过选择IB中的视图控制器并转到“身份检查器”)以及您指定的“自定义类“那里也是:

enter image description here


对于SecondViewController,您应该将initWithNibName:bundle:方法替换为:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self) {
        self.title = NSLocalizedString(@"Events", @"Events");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }

    return self; 
}