我有一个Navigation Controller
,当我按下后退按钮时,我不想弹出视图,但我想显示UIAlertView
。我只是想在制作后弹出视图选择UIAlertView
!
我能怎么做?
我试图捕捉事件'后退按钮',但没有结果:(
答案 0 :(得分:3)
在 viewDidload 中创建一个UIBarButtonItem并将其添加到导航栏
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
创建一个在单击backButton时调用的方法
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Pop View!" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
}
编写此UIAlertView委托方法,该方法将在单击UIAlertView中的按钮时调用
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
注意:不要忘记在头文件<UIAlertViewDelegate>
答案 1 :(得分:1)
您可以使用此viewWillDisappear
方法检查后退按钮(从here获取):
-(void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
希望这有帮助!
答案 2 :(得分:0)
在UIAlertview委托方法中实现pop clickedButtonAtIndex: 后退按钮需要实现仅显示UIAlertview
参考this
答案 3 :(得分:0)
按照此代码
按钮操作。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Do you want to open?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//NO
{
//your code
}
else if(buttonIndex==1)//YES
{
// your code
}
}