如何在Swift中实现以下Objective-C代码

问题描述 投票:-2回答:1

在我的初始视图控制器中,未调用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;
}
objective-c swift uinavigationcontroller delegates viewwillappear
1个回答
0
投票

您想这样声明您的MyRootViewController的Swift版本:

class MyRootViewController: UIViewController, UINavigationControllerDelegate {
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.