2012-04-09 18 views
2

UIAlertViewでユーザーの選択に基づいてView Controllerを変更しようとしています。私は客観的なCに新しいし、いくつかの問題を抱えています。UIAlertViewでViewController間を移動する

ユーザーが「完了しました」を押すと、アプリは前のView Controllerに戻ることができます。ユーザーが「Scoreboard」を押すと、次のView Controllerに移動することができます。

UIAlertView *jigsawCompleteAlert = [[UIAlertView alloc] //show alert box with option to play or exit 
            initWithTitle: @"Congratulations!" 
            message:completedMessage 
            delegate:self 
            cancelButtonTitle:@"I'm done" 
            otherButtonTitles:@"Play again", @"Scoreboard",nil]; 
      [jigsawCompleteAlert show]; 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSLog(@"Cancel Tapped."); 
     [self.navigationController popViewControllerAnimated:YES]; 

    } 
    else if (buttonIndex == 1) { 
     NSLog(@"GO tapped."); 
     startDate = [NSDate date]; 
     // Create the stop watch timer that fires every 10 ms 
     stopWatchTimer = [NSTimer 
          scheduledTimerWithTimeInterval:1.0/10.0 
          target:self 
          selector:@selector(updateTimer) 
          userInfo:nil 
          repeats:YES]; 
    } else if (buttonIndex == 2) { 
     NSLog(@"Scoreboard tapped."); 
    } 

} 

popViewの部分が動作していて、アプリケーションが1つのView Controllerをナビゲートしています。私は、1つのView Controllerを前進させる方法を知らない。私はそれがpushViewControllerと関係があると知っていますが、これを行う方法を詳述している非常に古い矛盾した記事しか見ていません。

ありがとうございました。ありがとうございました。

答えて

6

新しいのUIViewControllerを作成し、スタックにプッシュ、このような何かする必要があります:あなたはストーリーボードを使用している場合(インスペクタ属性)その後、ストーリーボードファイルにあなたのUIViewControllerの識別子を提供

else if (buttonIndex == 2) { 
    NSLog(@"Scoreboard tapped."); 
    MoreDetailViewController *ctrl = [[MoreDetailViewController alloc] initWithNibName:@"MoreDetailViewController" bundle:nil]; 

    [self.navigationController pushViewController:ctrl animated:YES]; 
}  

enter image description here

ハンドラにこのコードを入れてください:

MoreDetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MoreDetailViewController"]; 
[self.navigationController pushViewController:vc animated:YES]; 
+0

感謝を。最初にView Controllerをインポートする必要がありますか?私はARCを使用しているので、私が使ったコードは 'UIViewController * ctrl = [[UIViewController alloc] initWithNibName:@" AssignmentViewController3 "bundle:nil]です。 [self.navigationController pushViewController:ctrl animated:YES]; '。私は現在viewcontroller2とviewcontroller3の間にセグを持っていないので、 "シーン到達不能"という警告が出ています。ファイルの名前はAssignmentViewController3.hとAssignmentViewController3.mですが、AssignmentViewController3がNIB名であるかどうかはわかりません。再度ありがとう – garethdn

+0

はい。 #import "AssignmentViewController3.h"行を追加します。代わりにUIViewControllerタイプAssignmentViewController3 – beryllium

+0

私はSIGABRTエラーを取得しています - 'NIBをバンドル内に.....名前AssignmentViewController3'でロードできませんでした。私が今使っているコードは 'AssignmentViewController3 * ctrl = [[AssignmentViewController3 alloc] initWithNibName:@" AssignmentViewController3 "bundle:nil]です。 [self.navigationController pushViewController:ctrl animated:YES]; '。私は明示的にNIB名を設定したことがないので、これが起こっているかどうかは分かりません。 – garethdn

2

は、単純に次のように呼び出します。

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

ビューコントローラが設定されていない場合は、あなたがあなたのヘッダファイルにIBOutletを使用してInterface Builderを通って上方に、プログラム1またはリンクを作成する必要があります。

関連する問題