使用UIAlertView在ViewControllers之间导航

时间:2012-04-09 14:29:19

标签: iphone objective-c ios uialertview pushviewcontroller

我正在尝试根据用户在UIAlertView中的选择来更改视图控制器。我是目标c的新手,我遇到了一些麻烦。

当用户按下“我已完成”时,我希望应用程序导航回上一个视图控制器。当用户按下“记分板”时,我希望应用程序导航到下一个视图控制器。

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有关,但我只看到了相当古老而矛盾的文章,详细说明了如何做到这一点。

感谢您的帮助,谢谢。

2 个答案:

答案 0 :(得分:6)

您需要创建新的UIViewController,然后将其推送到堆栈,如下所示:

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

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

如果您正在使用StoryBoard,则在storyboard文件(Attribute Inspector)中为您的UIViewController提供标识符。 enter image description here

将此代码放入处理程序:

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

答案 1 :(得分:2)

只需拨打以下电话:

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

如果您没有设置视图控制器,则需要以编程方式创建一个或使用头文件中的IBOutlet通过Interface Builder链接一个。

相关问题