0

2 番目の UITabbarItem を押すと、UIViewController を開こうとしています。

これが最初のViewControllerの私のコードです。UITabbarItem #1 (タグ 1) を押すと、Label.text が設定されます。#2 で、secondViewController に切り替えたいのですが (タグ 2) クラッシュします。誰でも理由を知っていますか?最初のブレークポイントは UIStoryboard にあります... (ストーリーボードは 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

エラー

4

1 に答える 1

1

いくつかあります。

  1. まだプロパティを作成していない場合UITabBarController

  2. View Controllerをプッシュしない場所から移動したくない場合をUITabBarController除き、View Controllerを手動でtabBarアイテムに設定するか、tabBarControllerでView Controller配列を再設定する必要があります。

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

    [newControllers addObject:second];

    [self.tabBarController setViewControllers:newControllers アニメート:NO];

  3. あなたUITabBarControllerは絵コンテにいますか?目的のタブの上で右クリック ボタンを押したままにしてコントローラーの上にドラッグし、そのコントローラーをタブの場所として設定します。これを行う場合は、上記の 1 と 2 を無視できます。

  4. UITabBarControllerタブごとに異なる機能を実行することを意図していないため、最初のタブにラベルを設定し、2 番目のタブに を表示するコーディング方法UIViewControllerは不適切ですが、それは無関係です。

これらすべての後、あなたが nil でUITabBarControllerあるため、アプリがクラッシュする可能性があります。または、 「SecondView は値コーディング準拠のエラーではありません」または同様のエラーが発生します。これは、プロパティがストーリーボードで存在しないアイテムに設定されていることを意味します。 . ストーリーボードでView Controllerを右クリックし、黄色の警告マーカーが表示されているかどうかを確認します。表示されている場合は、すべての横にある「X」をクリックします。UINavigationController

于 2013-09-29T21:34:17.930 に答える