访问插座时发现无

时间:2018-01-17 05:55:54

标签: ios swift storyboard iboutlet

我在故事板中创建了一个containerView,我将cntrl拖动到他的viewcontroller并创建了一个IBOutlet。

尝试访问此插座时出现错误

  展开时发现没有。

class NavigationController: UIViewController {

    @IBOutlet weak var containerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        controller1  = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        controller1.delegate=self

        self.containerView.addSubview(controller1.view)
    }
}

我可以确认它已连接,因为我看到了插座的点,当在编辑器上触摸它时,它会在故事板上闪烁。

尝试过清理,删除等等。

尝试加载viewWillAppear(_ animated: Bool)会得到相同的结果。

1 个答案:

答案 0 :(得分:0)

试试这个

我的出口:

@IBOutlet weak var containerView: UIView!

My StoryBoard: - 使用ContainerView的VC

enter image description here

StoryBoard: - 要在ContainerView中添加VC

enter image description here

为要添加的ViewController创建一个Object

private lazy var FirstObject: VC1 =
    {
        // Instantiate View Controller
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC1") as! VC1

        // Add View Controller as Child View Controller
        self.addChildViewController(viewController)
        return viewController
    }()

创建将在容器视图中添加VC对象的函数

private func add1(asChildViewController viewController: UIViewController)
    {
        // Configure Child View
        viewController.view.frame = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width, height: self.containerView.frame.size.height)

        // Add Child View Controller
        addChildViewController(viewController)
        viewController.view.translatesAutoresizingMaskIntoConstraints = true

        // Add Child View as Subview
        containerView.addSubview(viewController.view)

        // Notify Child View Controller
        viewController.didMove(toParentViewController: self)
    }

现在在DidLoad中只需致电

self.add1(asChildViewController: FirstObject)

输出:

enter image description here

同时检查第二个选项,您可以尝试

https://stackoverflow.com/a/48274222/6080920

无需使用容器视图为此目的,只需使用普通UIView检查链接

使用UIView的示例项目

https://drive.google.com/open?id=1M12Nv4PLNUePSJFHf7w8b08M2_mWcUEC

使用ContainerView的示例项目

https://github.com/iOS-Geek/ContainerViews.git