0



ViewControllerXcode を使用して iOS で簡単なアプリを作成しています。別のアプリをモーダルとして読み込もうとしています。モーダルをロードしているオリジンHomeScreenViewController(から継承) は、プロジェクトのストーリーボードに由来します。 次に、ボタンが押されたイベントへの応答として、このモーダルを次のようにロードします。UIViewController

-(IBAction)onAddButtonPressed:(UIButton *)sender {
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

このクラスMyAnotherViewControllerは、ナビゲーション バーとテキスト フィールドを表示する単純なクラスであるため、ストーリーボードには表示されません。コードは次のとおりです (部分的なコード、残りは Xcode の自動生成メソッドです):

@implementation MyAnotherViewController 

- (void)viewDidLoad {
    [self.navigationItem setTitle:@"Example"];
    [self.view addSubview:[[UITextView alloc]initWithFrame:self.view.bounds]];
}
@end

navigationItem問題は、何らかの理由で が表示されないことです (添付の画像でも確認できます) 。私はそれがそうではない
ことも検証しました。さらに、タイトルが実際に「Example」に設定されていることをデバッグ モードで確認できます。self.navigationItemnil

スクリーンショットでわかるように、UITextView は画面全体をキャプチャします。

あなたの助けは大歓迎です、
乾杯...

4

2 に答える 2

2

UIViewControllerのUINavigationItemプロパティは、ViewController が 内にある場合にのみ使用されるUINavigationControllerため、次のようになります。

-(IBAction)onAddButtonPressed:(UIButton *)sender {
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
    UINavigationController *navCtl = [[UINavigationController alloc] initWithRootController:vc];
    [self presentViewController:navCtl animated:YES completion:nil];
}
于 2013-08-02T15:12:19.727 に答える
0

MyAnotherViewControllerが のサブクラスでない場合UINavigationController、または を手動で追加していない場合はUINavigationItemUIViewControllerナビゲーション アイテムを表示できません。MyAnotherViewControllerたぶん、 を でラップしてみることができますUINavigationController

// Assume you have adopted ARC
-(IBAction)onAddButtonPressed:(UIButton *)sender {
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

そして、あなた-viewDidLoadの ofMyAnotherViewControllerでは、これを行うだけです:

-(void)viewDidLoad {
    self.title = @"Example";
    /*
     * Your other code
     */
}
于 2013-08-02T16:22:10.737 に答える