在我的初始视图控制器中,未调用viewWillAppear()方法。所以我想尝试这里提供的解决方案https://benscheirman.com/2011/08/when-viewwillappear-isnt-called/
但是以上链接提供了Objective-c中的代码。我试图迅速实施它,但我不愿意这样做。我并不完全了解委托和协议。如何迅速实施以下代码
@interface MyRootViewController : UIViewController
<UINavigationControllerDelegate> {
UIViewController *_lastViewController;
}
// methods
@end
@implementation MyRootViewController
// other stuff
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.delegate = self;
// ...
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (_lastViewController) {
[_lastViewController viewWillDisappear:animated];
}
[viewController viewWillAppear:animated];
_lastViewController = viewController;
}
您想这样声明您的MyRootViewController
的Swift版本:
class MyRootViewController: UIViewController, UINavigationControllerDelegate {
...
}