I have found few posts for this problem but none of them solved my issue.
Say like I've..
- ViewControllerA
- ViewControllerB
I tried to add ViewControllerB as a subview in ViewControllerA but, it's throwing an error like "fatal error: unexpectedly found nil while unwrapping an Optional value
".
Below is the code...
ViewControllerA
var testVC: ViewControllerB = ViewControllerB();
override func viewDidLoad()
{
super.viewDidLoad()
self.testVC.view.frame = CGRectMake(0, 0, 350, 450);
self.view.addSubview(testVC.view);
// Do any additional setup after loading the view.
}
ViewControllerB is just a simple screen with a label in it.
ViewControllerB
@IBOutlet weak var test: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
test.text = "Success" // Throws ERROR here "fatal error: unexpectedly found nil while unwrapping an Optional value"
}
EDIT
With the suggested solution from the user answers, ViewControllerB in ViewControllerA is going off the screen. Grey border is the frame I have created for the subview.
For Add and Remove ViewController
Please also check the official documentation on implementing a custom container view controller:
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html#//apple_ref/doc/uid/TP40007457-CH11-SW1
This documentation has much more detailed information for every instruction and also describes how to do add transitions.
Translated to Swift 3:
A couple of observations:
When you instantiate the second view controller, you are calling
ViewControllerB()
. If that view controller programmatically creates its view (which is unusual) that would be fine. But the presence of theIBOutlet
suggests that this second view controller's scene was defined in Interface Builder, but by callingViewControllerB()
, you are not giving the storyboard a chance to instantiate that scene and hook up all the outlets. Thus the implicitly unwrappedUILabel
isnil
, resulting in your error message.Instead, you want to give your destination view controller a "storyboard id" in Interface Builder and then you can use
instantiateViewController(withIdentifier:)
to instantiate it (and hook up all of the IB outlets). In Swift 3:You can now access this
controller
'sview
.But if you really want to do
addSubview
(i.e. you're not transitioning to the next scene), then you are engaging in a practice called "view controller containment". You do not just want to simplyaddSubview
. You want to do some additional container view controller calls, e.g.:For more information about why this
addChild
(previously calledaddChildViewController
) anddidMove(toParent:)
(previously calleddidMove(toParentViewController:)
) are necessary, see WWDC 2011 video #102 - Implementing UIViewController Containment. In short, you need to ensure that your view controller hierarchy stays in sync with your view hierarchy, and these calls toaddChild
anddidMove(toParent:)
ensure this is the case.Also see Creating Custom Container View Controllers in the View Controller Programming Guide.
By the way, the above illustrates how to do this programmatically. It is actually much easier if you use the "container view" in Interface Builder.
Then you don't have to worry about any of these containment-related calls, and Interface Builder will take care of it for you.
For Swift 2 implementation, see previous revision of this answer.
Thanks to Rob, Updated Swift 4.2 syntax
Thanks to Rob. Adding detailed syntax for your second observation :
And to remove the viewcontroller :