gpt4 book ai didi

ios - 在 iOS 中按下返回按钮时如何创建确认弹出窗口?

转载 作者:IT王子 更新时间:2023-10-29 08:14:28 25 4
gpt4 key购买 nike

我想在有人按下我的 iOS 应用程序的“后退”按钮时添加一个弹出窗口,询问用户是否真的想回来。然后,根据用户的响应,我想撤消操作或继续。我试图在我的 View 的 viewWillDisappear 函数中添加代码,然后编写适当的委托(delegate)但它不起作用,因为它总是更改 View 然后显示弹出窗口。我的代码是:

    -(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];
}

委托(delegate)实现是:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Yes"])
{
[super viewWillDisappear:_animated];
}
}

这根本不起作用。现在有没有人有更好的方法来做到这一点或者可能有什么问题?

非常感谢,

最佳答案

一次-viewWillDisappear:被称为,没有停止你的 viewController免于消失。

理想情况下,您应该覆盖 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;
}
}

注意:不要忘记声明 <UIAlertViewDelegate> 在这个 viewController.h 文件中

关于ios - 在 iOS 中按下返回按钮时如何创建确认弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com