如何使用带有委托的协议将ViewController中的字符串值传递到Container中?
protocol VCDelegate {
func passData(theData:String)
}
class ViewController: UIViewController {
var delegate : VCDelegate?
@IBAction func getRestaurantInformation(_ sender: Any) {
let ViewC = ViewController()
let ContainerV = ContainerView()
ViewC.delegate = ContainerV
ViewC.delegate?.passData(theData: "pass this text")
}
override func viewDidLoad() {
super.viewDidLoad()
}
class ContainerView: UIViewController, FirstVCDelegate {
func passData(theData: String) {
print(theData)
textLabelOut.text = theData //it leaves an error "found nil while implicitly unwrapping optional value"
}
@IBOutlet weak var textLabelOut: UILabel!
override func viewWillAppear(_ animated: Bool) {
}
}
我不断收到错误消息“在隐式展开Optional值时意外发现nil”。但是据我了解,价值并不为零。正在打印。它只是不会将其传递给文本标签
答案 0 :(得分:1)
您应该用
加载容器let containerV = self.storyboard!.......
不是
let containerV = ContainerView()
因为它使所有门店都没有了
在显示vc之前,请勿访问vc的插座
ViewC.delegate?.passData(theData: "pass this text")
因为即使您从情节提要中加载它,出口也将一直为零,直到它加载