当导航栏的返回按钮(返回上一屏幕,返回父视图)被按下时,我需要执行一些操作。
有没有一些方法可以实现捕获事件并触发一些动作,在屏幕消失之前暂停并保存数据?
当导航栏的返回按钮(返回上一屏幕,返回父视图)被按下时,我需要执行一些操作。
有没有一些方法可以实现捕获事件并触发一些动作,在屏幕消失之前暂停并保存数据?
我使用了Pedro Magalhães的解决方案,但是当我像这样在扩展中使用它时,navigationBar:shouldPop
没有被调用:
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
return self.topViewController?.navigationShouldPopOnBackButton() ?? true
}
但是在一个UINavigationController
子类中,相同的东西可以正常工作。
class NavigationController: UINavigationController, UINavigationBarDelegate {
func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
return self.topViewController?.navigationShouldPopOnBackButton() ?? true
}
- (void)viewWillDisapear:(BOOL)animated
来执行自定义工作,当当前可见视图控制器保留的视图即将消失时。不幸的是,这将涵盖推送和弹出两种情况。purrrminator
所说,elitalon
的答案并不完全正确,因为即使通过编程方式弹出控制器,your stuff
也会被执行。elitalon
提到的外,我还检查了是否正在进行编程式弹出:- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ((self.isMovingFromParentViewController || self.isBeingDismissed)
&& !self.isPoppingProgrammatically) {
// Do your stuff here
}
}
在程序中编程弹出前,您需要将该属性添加到控制器并将其设置为“YES”:
self.isPoppingProgrammatically = YES;
[self.navigationController popViewControllerAnimated:YES];
UIControl *leftBarItemControl = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 90, 44)];
[leftBarItemControl addTarget:self action:@selector(onLeftItemClick:) forControlEvents:UIControlEventTouchUpInside];
self.leftItemControl = leftBarItemControl;
[self.navigationController.navigationBar addSubview:leftBarItemControl];
[self.navigationController.navigationBar bringSubviewToFront:leftBarItemControl];
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (self.leftItemControl) {
[self.leftItemControl removeFromSuperview];
}
}
就这些!
7ynk3r的回答非常接近我最终使用的方法,但需要进行一些微调:
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
UIViewController *topViewController = self.topViewController;
BOOL wasBackButtonClicked = topViewController.navigationItem == item;
if (wasBackButtonClicked) {
if ([topViewController respondsToSelector:@selector(navBackButtonPressed)]) {
// if user did press back on the view controller where you handle the navBackButtonPressed
[topViewController performSelector:@selector(navBackButtonPressed)];
return NO;
} else {
// if user did press back but you are not on the view controller that can handle the navBackButtonPressed
[self popViewControllerAnimated:YES];
return YES;
}
} else {
// when you call popViewController programmatically you do not want to pop it twice
return YES;
}
}
self.navigationController.isMovingFromParentViewController在iOS8和9上不再起作用,我使用:
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (self.navigationController.topViewController != self)
{
// Is Popping
}
}
终于找到解决方案了...我们一直在寻找的方法是UINavigationController的代理方法"willShowViewController"
//IMPORT UINavigationControllerDelegate !!
class PushedController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
//set delegate to current class (self)
navigationController?.delegate = self
}
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
//MyViewController shoud be the name of your parent Class
if var myViewController = viewController as? MyViewController {
//YOUR STUFF
}
}
}
MyViewController
和 PushedController
耦合在了一起。 - clozach