我有一个主Viewcontroller和一个Child Viewcontroller。
我意识到关闭Childviewcontroller时:
self.dismissViewControllerAnimated(true, completion: {
self.dismissViewControllerAnimated(true, completion: nil);
});
它只会关闭子视图控制器,我可以看到主视图控制器。没有其他代码被处理
如果我通过主视图控制器的通知关闭子视图控制器:
self.dismissViewControllerAnimated(true, completion: {
NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil);
self.dismissViewControllerAnimated(true, completion: nil);
});
然后在主视图控制器中调用我的refreshtextviewer_with_bookmark()函数,并且同时调用标准的viewDidLoad()。
这是正常行为,在这种情况下,在解除子视图控制器后调用viewDidLoad()吗?有办法防止这种情况吗?
答案 0 :(得分:2)
解雇后不应该打电话。经过测试,确实没有被称为。
以下是快速示例:
PARENT
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__) } }
子
class ChildViewController: UIViewController { @IBAction func exitTap(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: { () -> Void in NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil) }) } }
FYI __FUNCTION__
宏将替换为函数名称。