我的swift项目有许多带有xib和相关swift文件的屏幕。现在我正在设计初始屏幕,底部有UISegmentedControl。 UISegmentedControl有2个段,即主屏幕和我的个人资料屏幕。我有主屏幕和我的个人资料屏幕设计。我想将这两个屏幕添加到初始屏幕。我已经尝试使用以下代码将屏幕添加到初始屏幕视图,但它会抛出错误“致命错误:在解开可选值时意外发现nil” 代码我尝试过:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController")
self.addChildViewController(controller)
controller.view.frame = CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)
self.view.addSubview(controller.view)
controller.didMoveToParentViewController(self)
解决方案:
点击家庭分页
let homeViewController: HomeViewController = HomeViewController()
homeViewController.loadView()
homeViewController.viewDidLoad()
homeViewController.view.frame = self.subView1.bounds;
self.subView1.addSubview(homeViewController.view)
self.addChildViewController(homeViewController)
menuViewController.didMoveToParentViewController(self)
点击我的个人资料片段:
let profileViewController: ProfileViewController = ProfileViewController()
profileViewController.loadView()
profileViewController.viewDidLoad()
profileViewController.view.frame = self.subView2.bounds;
self.subView2.addSubview(profileViewController.view)
self.addChildViewController(profileViewController)
profileViewController.didMoveToParentViewController(self)