当用户在我的UINavigationController视图上按下Back-Button时,我想显示一个有两个选项的UIAlertView。一个选项(OK)允许用户返回(前一个屏幕),另一个选项(Cancel)将停留在当前控制器。
我在我的viewWillDisappear中实现了以下代码(我在so上找到了它):
-(void) viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"All Data will be lost"
message:@"You must be connected to the internet to use this app."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.tag = 1;
[alert show];
[alert release];
}
[super viewWillDisappear:animated];
}问题是,在我按下Back后,视图立即跳转到前一个屏幕,并在此屏幕上显示UIAlertView。
在UIAlertView回调方法中,我使用了以下代码,但没有发生任何事情(我认为这是,因为我已经在上一个视图的这一点上):
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1)
NSLog(@"back");
//Go Back ...
}BR,mybecks
发布于 2011-12-05 18:00:48
当viewWillDisappear被调用的时候已经太晚了。导航控制器已经解除了视图控制器。
相反,您需要拦截back按钮触摸事件。你可以通过多种方式来实现这一点--你可以使用一个自定义的后退按钮来显示你的UIAlertView。或者,您可以使用具有navigationBar:shouldPopItem:方法的UINavigationBarDelegate。
发布于 2011-12-05 18:35:51
在back按钮方法中编写UIAlertView的代码,当您单击back按钮时,它只是显示警报,当您单击按钮时,它只是调用clickedbuttonindex:方法,在此方法中,您只需调用所需的视图。
-(IBAction)back-button{
//alert
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//call the view
}https://stackoverflow.com/questions/8383463
复制相似问题