簡體   English   中英

如何從情節提要中的NIB子視圖選擇視圖控制器

[英]How do I segue from a NIB subview to a viewcontroller in the storyboard

我將情節提要和NIB Viewcontrollers結合在一起,如何才能彼此選擇

這是場景:

         UITabBarController(storyboard)|- UINavigationController (subclassed in storyboard) -> [UIViewController (NIB)] -> UIViewController (in Storyboard)

您必須為情節提要中的UIViewController賦予一個“ Storyboard ID”(在“身份檢查器”選項卡中)。 然后像這樣在您的NIB ViewController的代碼中實例化該ViewController:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerId"];
[self.navigationController pushViewController:vc animated:YES]; 

在UINavigationController子類中,您需要在NIB中實例化其視圖控制器的視圖控制器(我們將其稱為NIBViewController),並使用setViewControllers:animated:方法setViewControllers:animated:視圖控制器設置為rootViewController。 awakeFromNib這樣做:

- (void)awakeFromNib
{
    [super awakeFromNib];

    NIBViewController *viewController = [[NIBViewController] init];// It will look for the appropriate XIB as long as you did not change its name and it's in the main bundle, otherwise use initWithNibName:bundle:

    [self setViewControllers:@[viewController] animated:NO];
}

然后,要從NIBViewController轉到情節提要中包含的其他視圖控制器,只需在情節提要中為該視圖控制器設置一個標識符,實例化它,然后根據需要推送或顯示即可。

重要的是要標記為在您的NIBViewController實例中self.storyboard將為nil,因為該視圖控制器未包含在情節提要中,因此您需要實例化新的情節提要(解決方案A **)或從導航控制器獲取情節提要(解決方案B):

// Solution A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

// Solution B
UIStoryboard *storyboard = [self.navigationController storyboard];

UIViewController *storyboardViewController = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerId"];

[self.navigationController pushViewController:storyboardViewController animated:YES];

**請注意,這是在創建情節提要的新實例,因此最好不要嘗試棘手的事情,並避免與情節提要中包含的以前的視圖控制器發生沖突,否則您會發現這些視圖控制器不是相同的...我想到的第一個解決方案,但是絕對您應該尋求解決方案B。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM