简体   繁体   English

弹出 iOS UINavigationController 时如何调用方法?

[英]How to call a method when an iOS UINavigationController has been popped?

I have an iOS UINavigationController whose rootViewController is ViewController1 .我有一个 iOS UINavigationController ,其rootViewControllerViewController1 ViewController1 pushes ViewController2 by calling pushViewController(_:animated:) , and then ViewController2 pops itself by calling popViewController(animated:) . ViewController1通过调用pushViewController(_:animated:)推送ViewController2 ,然后ViewController2通过调用popViewController(animated:)弹出自身。 I am not using a storyboard.我没有使用 storyboard。

Now that I have arrived back in ViewController1 , I would like ViewController1 to do something automatically.现在我已经回到ViewController1 ,我希望ViewController1自动执行一些操作。

How can I arrange to have a method of ViewController1 called when we have returned there from ViewController2 ?当我们从ViewController2返回时,如何安排调用ViewController1的方法? In other words, how can ViewController1 sense that it has just been uncovered?换句话说, ViewController1怎么能感觉到它刚刚被发现呢? I know I can do this by calling the navigationController(_:didShow:animated:) method of a UINavigationControllerDelegate , but is there a simpler way?我知道我可以通过调用UINavigationControllerDelegatenavigationController(_:didShow:animated:)方法来做到这一点,但是有更简单的方法吗?

You can create a protocol that lets you notify ViewController1 when ViewController2 is dismissed, somewhat like this:您可以创建一个协议,让您在 ViewController2 被解除时通知 ViewController1,有点像这样:

protocol FeedbackDelegate: class {
    func shouldDoSomething()
}

class ViewController1: UIViewController, FeedbackDelegate {
    func thisIsWhereYouPresentVC2() {
        let vc = ViewController2()
        vc.feedbackDelegate = self
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: - FeedbackDelegate
    func shouldDoSomething() {
    }
}

class ViewController2: UIViewController {
    weak var feedbackDelegate: FeedbackDelegate?
    
    func thisIsWhereYouDismissVC2() {
        self.feedbackDelegate?.shouldDoSomething()
        self.navigationController?.popViewController(animated: true)
    }
}

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

相关问题 推入UINavigationController后,ViewController会立即弹出 - ViewController popped immediately after been pushed in UINavigationController UINavigationController 必须在使用新的 ViewController 更新视图时调用方法 - UINavigationController has to call method when updates view with a new ViewController 如何从UITableViewCell内部知道何时弹出其父表视图? - How can I tell from inside a UITableViewCell when its parent table view has been popped? 从导航堆栈中弹出该视图后,如何使后台任务完成 - How do make background task complete when that view has been popped from the navigation stack 你能检测到 UIViewController 何时被解除或弹出吗? - Can you detect when a UIViewController has been dismissed or popped? iOS Voice Over在UINavigationController上按下/弹出时宣布新的视图标题 - iOS Voice Over to announce new view titles when pushed/popped on a UINavigationController UINavigationController:弹出时释放ViewController的内存 - UINavigationController: Release ViewController's memory when popped 如何在 UINavigationController 中获得弹出视图的通知? - How to get notified of popped view in UINavigationController? iOS如何在弹出顶视图控制器时以编程方式检测? - iOS how to detect programmatically when top view controller is popped? 如何检测UINavigationController动画何时完成? - How to detect when UINavigationController animation has finished?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM