0

我有一个5-6视图控制器在我的应用程序,我正在使用导航控制器。现在我想要做的是:从进入一个视图控制器到另一个视图控制器,出现一个UIAlertView框并点击“否”,它应该弹出视图控制器并转到前一个。我曾尝试过PopToViewController:动画和PopViewController:动画,但他们都没有工作。有谁能告诉我这是否可能?如果是,如何?从UIAlertView弹出查看警报

当在警告框上按“否”时,我可以在控制台上打印任何内容,但视图不会弹出。有什么建议么?

感谢 维克

+0

设置动画参数为NO,然后尝试 – 2011-05-03 05:45:21

回答

0

答案假定通过“视图”你指的是一个UIViewController(因为你只能推/流行控制器)和你正在你的导航控制器上调用该控制器由pushViewController:animated:选择。

鉴于这种情况,你必须实现在控制器UIAlertViewDelegate .h文件中

@interface MyController : UIViewController <UIAlertViewDelegate> { 

} 

@end 

然后,确保正确检查一下按键的指标,当您创建UIAlertView中

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"No", nil]; 

[alert show]; 
[alert release]; 

在这个例子中在上面,“OK”按钮是索引0,“No”按钮是索引1.因此,当你实现alertview:clickedButtonAtIndex:委托方法时,你可以检查用户是否敲击了正确的按钮,然后执行控制器弹出操作。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { //button NO licked 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 

} 

编辑:

确保你实例化你的UINavigationController,否则,你的控制器navigationController财产将是零

tabBarController = [[UITabBarController alloc] init]; 
tabBarController.view.frame = CGRectMake(0, 0, 320, 460); 
NSMutableArray *controllers = [[NSMutableArray alloc] initWithCapacity:1]; 
ownerController *oController = [[ownerController alloc] init]; 

//navigation controller creation 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: oController]; 
[oController release]; 
//adds navigation controller to collection of controllers 
[controllers addObject:nav]; 

[tabBarController setViewControllers:controllers]; 
[controllers release]; 
[self.view addSubview:[tabBarController view]]; 
+0

@Felipe - 我做的完全一样的东西。我可以在控制台上打印东西,但popViewControllerAnimated:功能不起作用。以下是相关的代码: – Vik 2011-05-03 01:45:21

+0

- (void)viewDidLoad {super viewDidLoad]; \t self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; UIAlertView * alertOwner = [[UIAlertView alloc] initWithTitle:@“你是老板?” message:@“Owner Please”委托:self cancelButtonTitle:@“No”otherButtonTitles:@“Yes”,nil]; [alertOwner show]; [alertOwner发布]; \t \t } - (无效)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex { \t如果(buttonIndex == 0) \t { \t \t [self.navigationController popViewControllerAnimated:YES]; \t \t }} – Vik 2011-05-03 01:47:52

+0

你可以显示instantiante和调用这个控制器的代码吗? – 2011-05-03 10:42:30