首页 > 解决方案 > 以编程方式设置 Storyboard ID

问题描述

我在一个团队中工作,我需要的一个故事板是以编程方式完成的,这很好。我目前正在尝试使用委托将数据从一个视图控制器(以编程方式完成的 VC)传递到另一个(界面构建器构建的故事板)。

一个 VC(发件人)的代码是:

protocol setDelegate {
    func passData(_ area: String, _ location: String)
}

class ProgramaticVC : UIViewController {
    var delegate: setDelegate?

    ...

    func passData() {
        self.delegate?.passData(area, location)
        let interfaceBuilderVC = UIStoryboard.init("Main", nil).instantiateViewController("interfaceBuilderVC") as! InterfaceBuilderVC
        show(interfaceBuilderVC, nil)
    }

接收方代码:

class InterfaceBuilderVC : UIViewController {

    ...

func passData(_ area: String, _ location: String) {
     areaLabel.text = area
     locationLabel.text = location
     // Not sure how to set the delegate, but what I was thinking:
     let programmaticVC = UIStoryboard.init("Main", nil).instantiateViewController("CAN'T SET ID") as! ProgrammaticVC
     programmaticVC.delegate = self
}

所以我要么希望以编程方式设置情节提要的 ID,要么就如何正确设置我的委托获得任何其他建议。

标签: iosswift

解决方案


如果您只想将数据从一个视图控制器发送到另一个视图控制器,则没有理由使用委托。你可以简单地这样做:

class ProgramaticVC : UIViewController {

    func passData() {
        let interfaceBuilderVC = UIStoryboard.init("Main", nil).instantiateViewController("interfaceBuilderVC") as! InterfaceBuilderVC
        interfaceBuilderVC.area = area
        interfaceBuilderVC.location = location
        show(interfaceBuilderVC, nil)
    }
}

class InterfaceBuilderVC : UIViewController {

    var area: String?
    var location: String?
}

InterfaceBuilderVC如果您需要与 进行交流,您可能想要使用委托ProgramaticVC,但您的问题并不清楚这就是您想要做的。


推荐阅读