插座零,即使在同一个控制器

时间:2018-01-17 15:38:09

标签: swift function uiview uiviewcontroller outlet

我有一个带子视图的导航栏视图控制器,我试图从子视图中调用此函数

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    selectedFavour = FavoursDataSet[indexPath.row].id

    subviewviewcontrollername = "FavourView"

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

    let tabViewController: TabViewController = storyboard.instantiateViewController(withIdentifier: "TabView") as! TabViewController;
    wassubview = true

    tabViewController.showsubviewsubs()



}

调用showubviewssub()函数,这是函数

@IBOutlet var thecontentView: UIView!




func showsubviewsubs(){

    let previousVC = viewControllers[selectedIndex]

    previousVC.willMove(toParentViewController: nil)
    previousVC.view.removeFromSuperview()
    previousVC.removeFromParentViewController()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    subviewviewcontroller = storyboard.instantiateViewController(withIdentifier:"HomeView")

    let vc = subviewviewcontroller

    self.addChildViewController(vc!)
    vc?.view.frame = thecontentView.bounds
    thecontentView.addSubview((vc?.view)!)

    vc?.didMove(toParentViewController: self)

}

问题是thecontentView出口(调用此函数的子视图已经在视图中)返回nil,即使我从该Viewcontroller调用它

1 个答案:

答案 0 :(得分:0)

此时不会设置IBOutlets。您应该将showsubviewsubs的调用移动到“HomeView”视图控制器本身,因为此时您可以保证所有内容都已设置完毕。

这样做也是一种更好的做法,因为那时你不依赖视图控制器外部的东西来负责设置的完成。