myb*_*cks 2 xcode objective-c uinavigationcontroller ios
当用户按下我的UINavigationController视图上的后退按钮时,我想显示带有2个选项的UIAlertView.一个选项(OK)允许用户返回(到前一个屏幕),另一个选项(Cancel)将保留在当前控制器上.
我在viewWillDisappear中实现了以下代码(我在这里找到了):
-(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];
}
Run Code Online (Sandbox Code Playgroud)
问题是,在我按下Back后,视图立即跳转到previos屏幕并在此屏幕上显示UIAlertView.
在UIAlertView回调方法中,我使用了以下编码,但没有任何反应(我认为这是因为我之前已经在上一个视图中):
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1)
NSLog(@"back");
//Go Back ...
}
Run Code Online (Sandbox Code Playgroud)
BR,mybecks
到时候viewWillDisappear被称为为时已晚.导航控制器已经解除了视图控制器.
相反,您需要拦截后退按钮触摸事件.您可以通过多种方式执行此操作 - 您可以使用自定义后退按钮来显示您的UIAlertView.或者你可以使用UINavigationBarDelegate,有一个navigationBar:shouldPopItem:方法.
| 归档时间: |
|
| 查看次数: |
3551 次 |
| 最近记录: |