I want to access label of container view controller from parent view controller. I have tried the following:
prepareForSegue is a function in parent view controller.
ViewController2 is the container view controller.
parin2 is the name of the label in the container view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
let vc1 = segue.destinationViewController as! ViewController2;
vc1.parin2.text=String("helo");
}
This gives the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
You didn't say exactly which line caused the error, but my guess is that it was let vc1 = segue.destinationViewController as! ViewController2 (by the way, please omit the ';'s in Swift). The as! appears to be failing because the destination view controller isn't a ViewController2. To verify this, set a breakpoint on that line, then examine the segue's destinationViewController property to see what kind of view controller it is. If it's not a ViewController2, then the problem is in your storyboard. Either you're getting a segue that you didn't expect, or the class of the destination in the storyboard isn't a ViewController2.
A better pattern for handling prepareForSegue is the following:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
// ALWAYS check the segue identifier first! If you forgot to put one on
// your segue in the storyboard, then you should see a compiler warning.
switch segue.identifier {
case "SegueToViewController2":
guard let destination = segue.destinationViewController as? ViewController2 else {
// handle the error somehow
return
}
default:
// What happens when the segue's identifier doesn't match any of the
// ones that you expected?
}
}
Related
I am developing in Swift.
And the following picture is my storyboard.
There has a Main view. The Main view will change the view to the Scan view and also pass the data to the Scan view when press the Scan (Right Bar button item).
And the identifier of the StoryBoard Segue is ScanView
I use the following code to pass the data from Main to the Scan
When press the Scan (Right Bar button item).
self.performSegueWithIdentifier("ScanView", sender: self)
And pass the data to the next view
//prepare jumping to next page
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier=="ScanView"){
let desViewController = segue.destinationViewController as! ScanViewController
desViewController.myCenteralManager = myCenteralManager
}
}
And it will crash at let desViewController = segue.destinationViewController as! ScanViewController and show the error like the following :
Could not cast value of type 'UINavigationController' (0x3960e0a8) to 'BLEConnect.ViewController' (0x5514c).
Can someone teach me how to solve the issue ? Thanks in advance.
The error message are pretty clear, you try to get segue.destinationViewController as ScanViewController while in fact it is a navigation controller. You need to get the navigation controller first and then use its topViewController property to get your targeted view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier=="ScanView") {
if let navController: UINavigationController = segue.destinationViewController as? UINavigationController {
if let desViewController: ScanViewController = navController.topViewController as? ScanViewController {
desViewController.myCenteralManager = myCenteralManager
}
}
}
}
Your NavigationController is (probably) in the wrong place in Interface Builder. Right now, when you segue, you are going to the UINavigationController and not the ScanViewController like you expect. This is why the cast fails, because you are trying to force the UINavigationController to be a ScanViewController.
To fix this, you should place your MainViewController in the UINavigationController and then segue straight to your ScanViewController.
This guide shows exactly how you can use Interface Builder, UINavigationController, and segues to achieve what you're trying to do.
You can embed your navigation controller to the main ViewController and perform a segue as you have done above. That should solve the problem.
The error message is displayed because you have type casted Navigation Controller instead of Scan ViewController .
I have a storyboard with two main view controllers (ViewController / SecondViewController) and a container with an embedSegue that displays the EmbeddedViewController view.
Desired Result
I would like to pass the 5 from ViewController to EmbeddedViewController using prepareForSegue (I don't see a need for delegation here, since the data is only going 1 way, and that's TO the EmbeddedViewController.) Once "Segue" button is pressed, the second view controller appears with the yellow VC embedded with 5 in it.
Note - SecondViewController has no user interaction in displaying EmbeddedViewController. It just shows up automatically.
Problem
I can't get a reference to EmbeddedViewController from the mainSegue prepareForSegue. Nor can I access any child View Controllers from SecondViewController. Thought I could access properties of EmbeddedViewController if I access the children VC's of SecondViewController. See code below.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mainSegue" {
let secondVCSegue = segue.destinationViewController as! SecondViewController
print("array -- \(secondVCSegue.childViewControllers)")
// Array prints nothing. Just a blank [] :(
}
}
Any tips are appreciated thanks.
I am using for the first time the Unwind Segue approach. I have multiple view controllers as can be seen in the picture below (a few of them shown of course). In the Edit Profile I have a Done button. Once clicked I trigger an IBAction that triggers an unwind segue.
Here is the code for the Done button in the nav bar:
#IBAction func unwindToMainViews(sender: UIStoryboardSegue) {
//let sourceViewController = sender.sourceViewController
self.performSegueWithIdentifier("unwindToMainSegue", sender: self)
}
and in the
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
I am doing the following to pass data from Edit Profile back to Home View
if (segue.identifier == "unwindToMainSegue") {
// pass data to next view
let tabCtrl = segue.destinationViewController as! UITabBarController
let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController
destinationVC.userObject = self.userObject;
}
When segue identifier is matched and code is executed (to transfer userObject from this controller to another), it triggers the error:
Could not cast value of type 'app.EditProfileViewController' (0x100b99d80) to 'UITabBarController' (0x104a1d030).
How can this error be fixed? I am surprised since i am casting to UITabBarController so thought it should work.
You don't return to the UITabBarController in an unwind segue. You return to the ViewController that triggered the original segue, or one if its ancestors.
As #jlehr stated in the comments:
Unwind segues don't return to anything; they dismiss any pushed and
presented view controllers between the source and destination view
controller. The destination is wherever the implementation of the
unwind method is found, regardless of how the source view controller
was presented.
To unwind to the viewController that triggered the original segue, you need to implement the #IBAction func returnToHere(segue: UIStoryboardSegue) function in the viewController you want to return to.
Then when you set up your Done button by dragging to the Exit icon, you select returnToHere from the pop-up.
To pass data back to the sourceViewController, give your unwind segue an identifier such as "unwindToSource", then override prepareForSegue in the viewController you are returning from:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "unwindToSource" {
let dvc = segue.destinationViewController as! SourceViewController
dvc.userObject = self.userObject
}
}
I'm trying to segue from one view controller to another, and pass data to the next controller. But I keep getting this error:
Could not cast value of type 'UITableViewController' to 'UINavigationController'
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "segueLogWaterData") {
// pass data to next view
let nav = segue.destinationViewController as! UINavigationController
let segueViewController = nav.topViewController as! WaterDataViewController
}
}
This happens with WaterDataViewController as a UITableViewController. I've also tried embedding WaterDataViewController into a UINavigationController but I get a similar error:
Could not cast value of type 'UITableViewController' to 'MyApp.WaterDataViewController'
What am I doing wrong?
Actually, the segue.destinationViewController isn't the navigation controller; rather, it is the view controller that the segue is opening. Since UITableViewController is a subclass of UIViewController, this code should work just fine:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "segueLogWaterData") {
let dest = segue.destinationViewController as! WaterDataViewController
//Assign to dest's properties
}
}
nav.topViewController is a UITableViewController. Apparently, WaterDataViewController is not a subclass of UITableViewController.
Do you mean to be using rootViewController instead?
I get the same error sometimes because of "omitting class" to my view controller in the storyboard.
I am trying to get an instance of my destinationViewController but always get the error of dynamiccastclassunconditional.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier{
if identifier == "update" {
var selectedItem: NSManagedObject = endPointList[self.tableView.indexPathForSelectedRow()!.row] as NSManagedObject
var endPointForm : SettingsEndpointCreateViewController = segue.destinationViewController as SettingsEndpointCreateViewController
}
}
}
endPointForm triggers the error. The ViewController is of Type UITableViewController. What is the issue here?
UPDATE:
I am trying to present the viewController modally. But if I do that I lose the navigation controls. That is why I embedded the SettingsEndpointCreateViewController inside a NavigationController.
Like shown in this screenshot.
The problem is that the destination view controller has UIViewController type and not SettingsEndpointCreateViewController. If you created that vc in interface builder, you probably missed to specify the custom class for it (from the identity inspector)