在 iOS 中按下后退按钮时如何创建确认弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a confirmation Pop Up when pushing Back button in iOS?
提问by recasens
I want to add a pop up when someone pushes the "Back" button of my iOS App, to ask the user if he really wants to come back. Then, depending on the user's response, I would like to undo the action or proceed. I've tried to add the code in the viewWillDisappear function of my view and then write the proper delegate but it doesn't work, because it always change the view and then show the pop up. My code is:
当有人按下我的 iOS 应用程序的“返回”按钮时,我想添加一个弹出窗口,询问用户是否真的想回来。然后,根据用户的反应,我想撤消操作或继续。我试图在我的视图的 viewWillDisappear 函数中添加代码,然后编写正确的委托,但它不起作用,因为它总是更改视图然后显示弹出窗口。我的代码是:
-(void) viewWillDisappear:(BOOL)animated {
_animated = animated;
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
UIAlertView *alert_undo = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
message:@"You could be loosing information with this action. Do you want to proceed?"
delegate:self
cancelButtonTitle:@"Go back"
otherButtonTitles:@"Yes", nil];
[alert_undo show];
}
else [super viewWillDisappear:animated];
}
And the delegate implementation is:
委托实现是:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Yes"])
{
[super viewWillDisappear:_animated];
}
}
This is not working at all. Does anybody now a better way to do it or what could be wrong?
这根本行不通。现在有没有人有更好的方法来做到这一点,或者可能有什么问题?
Thank you very much,
非常感谢,
采纳答案by recasens
Thanks for your answer, @staticVoidMan! I finally used your code with some modifications. The back button cannot be modified so one should create a additional button and hid the standard one. The only problem is the style of the new "Back" button, which is not equal than the standard one. The final code is:
感谢您的回答,@staticVoidMan!我终于使用了您的代码并进行了一些修改。后退按钮无法修改,因此应该创建一个附加按钮并隐藏标准按钮。唯一的问题是新的“返回”按钮的样式,与标准按钮不一样。最后的代码是:
- (void)viewDidLoad
{
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem = bbtnBack;
}
- (void)goBack:(UIBarButtonItem *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"...Do you want to proceed?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex) {
case 0: //"No" pressed
//do something?
break;
case 1: //"Yes" pressed
//here you pop the viewController
[self.navigationController popViewControllerAnimated:YES];
break;
}
}
回答by staticVoidMan
Once -viewWillDisappear:
is called, there's no stopping your viewController
from disappearing.
一旦-viewWillDisappear:
被调用,就无法阻止你viewController
的消失。
You should ideally, override the navigationBar
's back button and in it's method, display the alert (the rest being pretty much the same)
理想情况下,您应该覆盖navigationBar
后退按钮并在它的方法中显示警报(其余部分几乎相同)
- (void)viewDidLoad
{
//...
UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(goBack:)];
[self.navigationItem setBackBarButtonItem: bbtnBack];
}
- (void)goBack:(UIBarButtonItem *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"...Do you want to proceed?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex) {
case 0: //"No" pressed
//do something?
break;
case 1: //"Yes" pressed
//here you pop the viewController
[self.navigationController popViewControllerAnimated:YES];
break;
}
}
NOTE: Don't forget to declare <UIAlertViewDelegate>
in the .hfile of this viewController
注意:不要忘记<UIAlertViewDelegate>
在这个.h文件中声明viewController