Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中使用容器视图委派_Swift_Uiviewcontroller_Swift2_Uicontainerview_Swift2.1 - Fatal编程技术网

在Swift中使用容器视图委派

在Swift中使用容器视图委派,swift,uiviewcontroller,swift2,uicontainerview,swift2.1,Swift,Uiviewcontroller,Swift2,Uicontainerview,Swift2.1,我正在为iPad Pro开发一个应用程序。在此应用程序中,containerView用于添加其他视图,并与之交互 首先,我创建了一个协议: protocol DataViewDelegate { func setTouch(touch: Bool) } 然后,我创建了第一个视图控制器 } 最后,我创建了一个将嵌入containerView的视图 } 但由于某些原因,什么也没有发生,第一个视图控制器在setTouch功能中没有收到任何信息 我的问题是:在这种情况下,使用容器,如何在两

我正在为iPad Pro开发一个应用程序。在此应用程序中,
containerView
用于添加其他
视图
,并与之交互

首先,我创建了一个协议:

protocol DataViewDelegate {
    func setTouch(touch: Bool)
}
然后,我创建了第一个视图控制器

}

最后,我创建了一个将嵌入containerView的视图

}

但由于某些原因,什么也没有发生,第一个视图控制器在setTouch功能中没有收到任何信息


我的问题是:在这种情况下,使用容器,如何在两个ViewsController之间进行通信?

看起来您定义了委托,但尚未设置委托。这种情况经常发生在我身上。

就像@nwales说你还没有设置代理一样。您应该在第一个viewController(包含viewContainer)上的prepareForSegue函数中设置委托

首先选择嵌入序列并在属性检查器中设置标识符。 然后在parentViewController中实现func PrepareForegue,如下所示:

Swift 4+:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "the identifier") {
            let embedVC = segue.destination as! ViewController
            embedVC.delegate = self
        }
    }
下:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if (segue.identifier == "the identifier") {
        let embedVC = segue.destinationViewController as! ContainerViewController
        embedVC.dataViewDelegate = self
    }
}

要在视图控制器之间进行通信,您可以使用委派,就像您正在做的那样。但是,您应该阅读苹果的UIViewController编程指南以了解最佳实践,特别是题为“使每个视图控制器成为一个岛”的部分。我正处于协议和委托的早期阶段。如何设置代理?可以在父视图控制器的PrepareForegue中设置代理
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "the identifier") {
            let embedVC = segue.destination as! ViewController
            embedVC.delegate = self
        }
    }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if (segue.identifier == "the identifier") {
        let embedVC = segue.destinationViewController as! ContainerViewController
        embedVC.dataViewDelegate = self
    }
}