在 iOS 中离开视图之前要求用户确认 [英] Asking for user confirmation before leaving a view in iOS

查看:26
本文介绍了在 iOS 中离开视图之前要求用户确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户离开某个视图之前显示一个 UIAlertView,方法是点击返回"导航栏按钮或点击我拥有的选项卡栏中的选项卡项之一,在为了向他求证.这将是一个两个按钮的警报,一个取消"一个留在视图中,一个接受"一个离开.我需要这样做,因为我必须让用户知道如果离开,未保存的更改将会丢失.

I need to show an UIAlertView before a user leaves a certain view, either by tapping a 'back' navigation bar button or by tapping one of the tab items in the tab bar I have, in order to ask him for confirmation. It would be a two-button alert, a 'Cancel' one to stay in the view, and an 'Accept' one to leave. I need to do this because I have to make the user aware that unsaved changes will be lost if leaving.

我尝试通过在 viewWillDisappear: 方法中创建和显示警报视图来做到这一点:

I tried to do this by creating and showing the alert view in the viewWillDisappear: method:

- (void)viewWillDisappear:(BOOL)animated
{

   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Exit", @"")
                                                    message:NSLocalizedString(@"Are you sure you want to leave? Changes will be discarded", @"")
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"Cancel", @"")
                                          otherButtonTitles:NSLocalizedString(@"Accept", @""), nil];

   [alertView show];

   [super viewWillDisappear:animated];
}

但是视图无论如何都会弹出,然后显示警报视图并且应用程序崩溃,因为它的委托是已经从导航堆栈中弹出的视图控制器......我没有找到解决这个问题的方法场景,有人可以帮我吗?

But the view is pop anyway, and the alert view is shown after that and app crashes since its delegate is the view controller that has been already pop from the navigation stack... I don't find the way to solve this scenario, can anybody help me?

谢谢!

推荐答案

当 viewWillDissapear 不起作用时显示警报视图,因为视图已经消失,它正在被删除.

Showing the alert view when viewWillDissapear won't work, because the view is already dissapearing, it's on its way to be removed.

您可以做的是在按下后退按钮时为自己添加一个自定义操作,然后您决定按下后退按钮时要执行的操作,您可以显示警报视图,然后在其中一个按钮中进行关闭视图,如下所示:

What you can do, is add yourself a custom action when the back button is pressed, then you decide what to do when the back button is pressed, you can show the alert view, and then in one of the buttons procedd to dismiss the view, something like this:

- (id)init {
    if (self = [super init]) {
    self.navigationItem.backBarButtonItem.target = self;
    self.navigationItem.backBarButtonItem.action = @selector(backButtonPressed:);
  }
    return self;
}

然后在按下后退按钮时显示警报视图:

Then show the alert view when the back button is pressed:

-(void)backButtonPressed:(id)sender
 {
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Exit", @"") message:NSLocalizedString(@"Are you sure you want to leave? Changes will be discarded", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"") otherButtonTitles:NSLocalizedString(@"Accept", @""), nil];      
    [alertView show];           
}

现在,当按下警报视图中的确认按钮时,只需调用:

Now, when the confirmation button in the alert view is pressed, just call:

[self.navigationController popViewControllerAnimated:YES];

如果用户取消或者什么都不做

Or do nothing if the user cancels

这篇关于在 iOS 中离开视图之前要求用户确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆