简体   繁体   English

通过“后退”按钮返回导航堆栈

[英]Going back in the Navigation Stack via the “Back” Button

I'd like to save some data to NSUserDefaults on the user leaving a table view in a UINavigation stack. 我想在UINavigation堆栈中保留表视图的用户上将一些数据保存到NSUserDefaults。 I'd like to be able to still utilize the default back button (the button with the chevron), while still having a reliable way to save the data. 我希望仍然可以使用默认的后退按钮(人字形按钮),同时仍具有可靠的方式来保存数据。 I've tried using the viewDidDisappear method, but I'm not sure how reliable this is. 我已经尝试过使用viewDidDisappear方法,但是我不确定这是多么可靠。 I'm worried that it might be called when the app is killed (or any other system action that might call the method). 我担心应用程序被杀死(或可能调用该方法的任何其他系统操作)时可能会调用它。 The only way to exit the view controller is via the back button. 退出视图控制器的唯一方法是通过“后退”按钮。 I'm not sure what else to try, if what I have tried is a technique that can be used, please do tell. 我不确定还有什么可以尝试的,如果我尝试过的是可以使用的技术,请告诉我。 Thanks. 谢谢。

Maybe using the UINavigationControllerDelegate will help you in this, so you will be aware of popping and pushing in your navigation controller. 也许使用UINavigationControllerDelegate会对此有所帮助,因此您将意识到弹出并推入导航控制器。

self.navigationController.delegate = self;

and in push or pop this delegate function will be called, you can check the view controller that will be presented. 然后在push或pop中调用此委托函数,您可以检查将要显示的视图控制器。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
      if (viewController.class == [YourPreviousViewController class]) {
           //back clicked, poping
       }
}

We came across the same issue and ended up using a custom nav bar button with an action. 我们遇到了同样的问题,最终使用了带有操作的自定义导航栏按钮。 It was fairly easy to mimic the default back arrow too. 模仿默认的后退箭头也相当容易。 The reason we decided to go this way instead is because we found we didn't have enough control over the saving of data otherwise, it was possible that the data might not save in time before the view was dismissed. 之所以决定采用这种方式,是因为我们发现我们对数据的保存没有足够的控制权,否则,数据可能无法在关闭视图之前及时保存。 Also, in our case, the user may not have wanted to save the changes and just wanted to back out of the view. 同样,在我们的情况下,用户可能不想保存更改,而只是想退出视图。 Using an action allowed us to present an alert to the user that the data was not saved and gave them the option to save or not. 通过执行操作,我们可以向用户发出警报,通知您数据尚未保存,并为他们提供了保存或不保存的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM