一起使用笔尖和故事板

从我在互联网上阅读的内容来看,笔尖和故事板在同一个项目中可以相互比较。 现在我正在创build一个选项卡式应用程序,并希望执行以下操作:

表1:用笔尖build造,

表2:用故事板构build,

表3:用笔尖构build

最初我用nib创build了所有东西,所以在我的委托中,我使用下面的代码从一个选项卡传递给下一个选项卡:

- (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; } 

你可以通过创build一个故事板然后在其中创build一个SecondViewController场景来做你的build议。 在代码中,你将会用下面的代码replaceinitWithNibName:调用storyboardWithName:instantiateViewControllerWithIdentifier:

然而,由于故事板的一个主要特点是能够将多个控制器绑定在一起并定义它们之间的关系,所以很难想象为什么为一个控制器做这件事是一个好主意。

正如菲利普所说,只要使用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的“Storyboard ID”(你通过在IB中select视图控制器并去“Identity Inspector”)来做到这一点,并且你在那里指定了你的“自定义类”也是:

在这里输入图像说明


对于SecondViewController ,你应该把你的initWithNibName:bundle:方法replace成:

 - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { self.title = NSLocalizedString(@"Events", @"Events"); self.tabBarItem.image = [UIImage imageNamed:@"second"]; } return self; }