我有一个主视图 Controller 和一个 subview Controller 。
我意识到当关闭 Childviewcontroller 时:
self.dismissViewControllerAnimated(true, completion: {
self.dismissViewControllerAnimated(true, completion: nil);
});
它只会关闭 subview Controller ,我可以看到主视图 Controller 。不处理其他代码
如果我关闭 subview Controller 并向主视图 Controller 发出通知:
self.dismissViewControllerAnimated(true, completion: {
NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil);
self.dismissViewControllerAnimated(true, completion: nil);
});
然后我的refreshtextviewer_with_bookmark()函数在主视图 Controller 中被调用,同时标准viewDidLoad()也被调用。
在这种情况下,在关闭 subview Controller 后调用 viewDidLoad() 是正常行为吗?有没有办法防止这种情况发生?
最佳答案
解散后不应再调用。经过测试,确实没有被调用。
这是一个简单的示例:
家长
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() println(__FUNCTION__) NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshtextviewer_with_bookmark", name: "refreshtextviewer_with_bookmark", object: nil) } deinit { NSNotificationCenter.defaultCenter().removeObserver(self) } @IBAction func buttonTap(sender: AnyObject) { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("childVC") as! ChildViewController self.presentViewController(vc, animated: true, completion: nil) } func refreshtextviewer_with_bookmark() { println(__FUNCTION__) } }
child
class ChildViewController: UIViewController { @IBAction func exitTap(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: { () -> Void in NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil) }) } }
仅供引用 __FUNCTION__
宏被替换为函数名称。
关于ios - viewdidload 关闭后调用,关闭 subview Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810526/