Swift - 如何在调用 removeFromSuperview() 后得到通知?
Swift - How to get notified after removeFromSuperview() has been called?
我有两个视图控制器。应用程序启动时会加载第一个 VC,然后如果点击某个按钮,则会使用以下代码调用 secondVC:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tagsStory") as! TagsVC
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
在 secondV 中,当点击某个按钮时,secondVC 被删除,firstVC 通过以下代码返回给用户:
self.view.removeFromSuperview()
我的问题是,当第一个 VC 中的第二个 VC 被删除时,我如何得到通知?
我尝试使用 viewDidAppear、viewWillAppear、willMove...但还没有任何效果。
在第二个 VC 中,当点击某个按钮时,第二个 VC 被删除,第一个 VC 返回给已经通知您的用户 ,如果是一对多的情况,您可以为此创建委托或触发通知。
如果您只想进行函数调用,只需使用通知即可。在您的父视图控制器中,注册通知以接收函数调用。
NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
func listnerFunction() {
tableView.reloadData()
}
当视图控制器被销毁时忘记删除这个监听器
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "notificationName"), object: nil)
}
然后在你的子视图控制器中,当你要从父视图控制器中删除它时,以这种方式调用注册的通知函数
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: nil)
UIViewController documentation 中对此进行了介绍。
你的firstVc是一个container view controller,所以它有一定的职责:
Your container view controller must associate a child view controller with itself before adding the child's root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child's root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container's implementation to provide the expected containment behavior.
在添加子视图之前通过调用 didMove(toParentViewControler:)
添加第二个视图控制器是正确的,但是在删除子视图之后还需要调用 removeFromParentViewController()
。
如果您这样做,那么将调用 viewDid/WillDisappear
方法。
我有两个视图控制器。应用程序启动时会加载第一个 VC,然后如果点击某个按钮,则会使用以下代码调用 secondVC:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tagsStory") as! TagsVC
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
在 secondV 中,当点击某个按钮时,secondVC 被删除,firstVC 通过以下代码返回给用户:
self.view.removeFromSuperview()
我的问题是,当第一个 VC 中的第二个 VC 被删除时,我如何得到通知?
我尝试使用 viewDidAppear、viewWillAppear、willMove...但还没有任何效果。
在第二个 VC 中,当点击某个按钮时,第二个 VC 被删除,第一个 VC 返回给已经通知您的用户 ,如果是一对多的情况,您可以为此创建委托或触发通知。
如果您只想进行函数调用,只需使用通知即可。在您的父视图控制器中,注册通知以接收函数调用。
NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
func listnerFunction() {
tableView.reloadData()
}
当视图控制器被销毁时忘记删除这个监听器
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "notificationName"), object: nil)
}
然后在你的子视图控制器中,当你要从父视图控制器中删除它时,以这种方式调用注册的通知函数
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: nil)
UIViewController documentation 中对此进行了介绍。
你的firstVc是一个container view controller,所以它有一定的职责:
Your container view controller must associate a child view controller with itself before adding the child's root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child's root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container's implementation to provide the expected containment behavior.
在添加子视图之前通过调用 didMove(toParentViewControler:)
添加第二个视图控制器是正确的,但是在删除子视图之后还需要调用 removeFromParentViewController()
。
如果您这样做,那么将调用 viewDid/WillDisappear
方法。