[英]Don't Pop Navigation Controller in Xcode if AlertView
I have a Navigation Controller
and when I press the back button I don't want to pop the view but I want to show an UIAlertView
.I just want to pop the view after having make a choice on the UIAlertView
! 我有一个
Navigation Controller
,当我按下后退按钮时,我不想弹出视图,但我想显示一个UIAlertView
。我只是想在选择UIAlertView
之后弹出视图! How can I do? 我能怎么做? I tried to catch the event 'back button pressed' but with no results :(
我试图捕获事件“按下后退按钮”,但没有结果:(
Create a UIBarButtonItem inside viewDidload and added it to the navigation bar 在viewDidload中创建一个UIBarButtonItem并将其添加到导航栏中
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
create a method which will be called when the backButton is clicked 创建一个单击backButton时将调用的方法
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Pop View!" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
}
write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked 编写此UIAlertView委托方法,当单击UIAlertView中的按钮时将调用该方法
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NOTE : Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>
注意 :不要忘记在头文件
<UIAlertViewDelegate>
添加UIAlertView委托。
You can check for back-button press in the viewWillDisappear
method like this (got it from here ): 您可以像这样在
viewWillDisappear
方法中检查是否按下了后退按钮(从此处获取它):
-(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];
Hope this helps! 希望这可以帮助!
follow this code 遵循此代码
in button action. 在按钮动作。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.