[英]Using nibs and storyboards together
根據我在互聯網上看到的內容,筆尖和故事板在同一個項目中相互比較。 現在我正在創建一個選項卡式應用程序,並希望執行以下操作:
標簽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; }
您可以通過創建故事板然后在其中創建SecondViewController
場景來執行您的建議。 在代碼中,您將使用對storyboardWithName:
和instantiateViewControllerWithIdentifier:
調用替換initWithNibName:
instantiateViewControllerWithIdentifier:
.
但是,由於故事板中的一個主要功能是能夠將多個控制器連接在一起並定義它們之間的關系,因此很難想象為什么為一個控制器執行此操作會是一個好主意。
正如Phillip所說,只需使用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.storyboard
和iPad.storyboard
,但更改上面的代碼以匹配您稱之為這兩個故事板的任何內容。 此外,這再次假定您定義了該場景以承載Second
的“故事板ID”(您通過在IB中選擇視圖控制器並轉到“Identity Inspector”來執行此操作)以及您在那里指定了“自定義類”也是:
對於SecondViewController
,您應該將initWithNibName:bundle:
method替換為:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.title = NSLocalizedString(@"Events", @"Events");
self.tabBarItem.image = [UIImage imageNamed:@"second"];
}
return self;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.