ios - viewdidload 关闭后调用,关闭 subview Controller

标签 ios swift uiviewcontroller

我有一个主视图 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/

相关文章:

iphone开发: getting EXC_CRASH (SIGABRT) randomly

ios - viewDidAppear 适用于 iOS 8.1 及更高版本

ios - 想要创建一个监听器来检测整个应用程序中的 viewWillAppear 调用

ios - 执行 Push Segue 时是否应该释放 UIViewController

android - 如何实现可靠的推送消息服务?

ios - MapBox 标记偏移

ios - TabBarController 添加自定义按钮不可点击问题

ios - 检查网络服务器上数据更新的最佳方法

iphone - 什么时候需要使用 View Controller ?

iphone - UIViewController 多个 UIView