2012-04-09 76 views
2

我試圖根據用戶在UIAlertView中的選擇來更改視圖控制器。我是客觀的新手,並且遇到了一些麻煩。使用UIAlertView在ViewControllers之間導航

當用戶按下「我完成了」時,我希望應用程序導航回到先前的視圖控制器。當用戶按下「Scoreboard」時,我希望應用程序導航到下一個視圖控制器。

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部件正在工作,應用程序導航回一個視圖控制器。我不知道如何讓應用程序向前移動一個視圖控制器。我知道這與pushViewController有關,但我只看到很老的和矛盾的文章,詳細說明如何做到這一點。

我很感謝這個幫助,謝謝。

回答

6

您需要創建新的UIViewController,然後將其推到堆棧,這樣的事情:

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

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

如果您使用的故事板,然後在故事板文件(屬性檢查器)UIViewController中提供標識符。 enter image description here

把這段代碼到處理程序:

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

謝謝。我是否需要先導入視圖控制器?我正在使用ARC,因此我使用的代碼是'UIViewController * ctrl = [[UIViewController alloc] initWithNibName:@「AssignmentViewController3」bundle:nil]; [self.navigationController pushViewController:ctrl animated:YES];'。我目前在viewcontroller2和viewcontroller3之間沒有segue,所以我得到了「scene unreachable」警告。這些文件名爲AssignmentViewController3.h和AssignmentViewController3.m,但我不確定這是否意味着AssignmentViewController3是NIB名稱。再次感謝 – garethdn 2012-04-09 15:00:15

+0

是的。添加行#import「AssignmentViewController3.h」。而不是UIViewController類型AssignmentViewController3 – beryllium 2012-04-09 15:07:36

+0

我得到一個SIGABRT錯誤 - '無法加載NIB束.....名稱AssignmentViewController3'。我現在使用的代碼是'AssignmentViewController3 * ctrl = [[AssignmentViewController3 alloc] initWithNibName:@「AssignmentViewController3」bundle:nil]; [self.navigationController pushViewController:ctrl animated:YES];'。我不確定是否發生這種情況,因爲我從來沒有明確設置NIB名稱,因爲我不知道如何? – garethdn 2012-04-09 15:15:30

2

只需調用如下

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

如果你沒有一個視圖控制器設置,則需要以編程方式創建一個或鏈接一起來通過界面生成器在你的頭文件使用IBOutlet中。