如何从uiview容器控制器获取父控制器

时间:2014-12-28 12:01:53

标签: ios swift uiviewcontroller

这是我的设置:

UIViewController with a UIViewContainer

我有一个UIViewController,里面有一个显示UITableViewController的UIViewContainer。

当有人从UITableViewController按下一个单元格时,我会进行一些处理,然后我想解除父UIViewController,但我无法访问它。

我在UITableViewController中尝试了以下内容:

self.parentViewController //It is nil
self.presentingViewController //It is nil

该项目适用于iOS7和iOS8的Xcode 6。我在 Swift 工作。

1 个答案:

答案 0 :(得分:2)

使用属性parentDelegate / childDelegate进入旧学校。

//In the parent
class ParentViewController: UIViewController {
    var child: ChildViewController?

    //Set the child delegate
    //And child's parent delegate
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "embedTable" {
            self.child = (segue.destinationViewController as ChildViewController)
            (segue.destinationViewController as ChildViewController).parent = self
        }
    }

    //After dismissing view, set delegates to nil so they can correctly release
    func exit() {
        self.dismissViewControllerAnimated(true, completion: {() in
            self.child?.parent = nil
            self.child = nil
        })
    }
}

//In the child
class ChildViewController: UITableViewController {
    var parent: ParentViewController?
}