簡體   English   中英

從UITabbarItem和Storyboard創建一個UIViewController

[英]Create a UIViewController from UITabbarItem and Storyboard

如果我按第二個UITabbarItem,則嘗試打開UIViewController。

這是我第一個ViewController的代碼。 如果我按UITabbarItem#1(標簽1),它將設置Label.text。 在#2,我想切換到secondViewController並使其崩潰(標簽2)。 有人知道為什么嗎? 第一個斷點位於UIStoryboard ...(Storyboard稱為:Main.storyboard)。 感謝幫助

#import "ViewController.h"
#import "secondViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tabBar.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    switch (item.tag) {
        case 1:        self.testlabel.text =@"test";
            break;
        case 2:
        {  // create the Storyboard object
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        secondViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"secondview"];
        [self.navigationController pushViewController:second animated:YES];
        }         
        break;
        default:         break; }
}
@end

錯誤

有幾件事。

  1. 如果尚未為UITabBarController設置屬性

  2. 除非您不想離開UITabBarController導航,否則不要按下視圖控制器,否則必須將視圖控制器手動設置為tabBar項,或者在tabBarController中重新設置視圖控制器數組。

    NSMutableArray * newControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];

    [newControllers addObject:second];

    [self.tabBarController setViewControllers:newControllers animation:NO];

  3. 您的UITabBarController在情節提要中嗎? 在所需的選項卡上按住鼠標右鍵並將其拖動到控制器上,然后將該控制器設置為選項卡的位置,如果這樣做,則可以忽略上面的1和2

  4. UITabBarController並不意味着每個選項卡都具有不同的功能,因此將第一個選項卡設置為標簽,而第二個選項卡顯示UIViewController則是不正確的編碼做法,但這是不相關的。

在所有這些UITabBarController之后,該應用程序可能會崩潰,因為您的UINavigationController為nil,或者您收到“ SecondView不是值編碼兼容錯誤”或類似內容,這意味着在情節提要中將屬性設置為沒有不再存在。 因此,在情節提要中右鍵單擊視圖控制器,查看是否看到任何黃色警告標記,如果是,請單擊所有警告標記旁邊的“ X”。

暫無
暫無

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

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