This question already has answers here:
Changing label text of second ViewController upon clicking button in first ViewController
(4 answers)
Closed 6 years ago.
When using the prepare (forSegue) method to pass data to the destination view controller of a segue, all members of the destination VC are nil. Thus, my program crashes when trying to access these members.
My code in the source view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Second2DetailEdit"{
var detailVC = segue.destination as! DrinkDetailViewController
detailVC.headerLabel.text = "Edit Drink"
}
//...
}
The VC itself is not nil. Only all members of the VC. Did I forget something?
This does only occur in my new Swift 3 project. Similar code in my Swift 2 projects have no problems.
Thanks in advance.
The destination view controller's view is not created yet and that's why all IBOutlets are nil. Try sending the information you need in normal properties like Strings and in ViewDidLoad of the destination, update your view.
The best way around this is to create a variable in your second view controller and set the text to that instead.
Then once the vc has initialized you can set the headerLabel.text to the new variable in viewDidLoad
I would suggest safely unwrapping your detailVC rather than force unwrapping...
if segue.identifier == "Second2DetailEdit" {
if let detailVC = segue.destination as? DrinkDetailViewController {
detailVC.headerLabel.text = "Edit Drink"
}
}
You'll have an easier time setting properties on the destinationVC and passing info to those rather than trying to do it through UILabels...
Related
I have two storyboards and I need from VC form second Storyboard change the background color on VC from the main storyboard.
I use this code in button on second storyboard:
let vc = FirstScreenVC()
vc.view.backgroundColor = .black
and have this error:
"Unexpectedly found nil while implicitly unwrapping an Optional value"
in FirstScreenVC file at viewDidLoad.
Debug tells that 3 UIElement on FirstScreenVC equals nil, but I see it and it first what load in app.
You have instantiated a new FirstScreenVC which is not the one that you're seeing. Depending on how your stack is set, you need to either fetch the existing viewController, or pass the reference to it to the viewController that needs to do the change.
If you use navigation controller, you can use prepareForSegue method when going to other page.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SecondViewController
vc.view.backgroundColor = UIColor.black
}
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I'm a beginner in programming, so got a question. I have an app with 3 viewControllers. First 2 has inputs. So, the question is, how can I pass data from 1st and 2nd VC to the third one? Thanks a lot for help.
Most use segues between each view controller typically (VC#1 >> VC2 >> VC3). If you are doing that, you simply need to declare public properties along the route and pass them.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Segue VC #2" {
if let vc = segue.destination as? SecondViewController {
vc.property1 = self.propertyToSend
}
}
}
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I'm making a new application.
If I have a text field on one view controller and a label on another (both View Controllers in ViewController class), How can i type something in on the text field and then take me to the next ViewController and display the text?
In the source controller in prepare for segue you set a property on your destination controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let detailViewController = segue.destination as? DetailViewController {
detailViewController.titleText = mylabel.text
}
}
In your destination controller's view did load you assign this property to your label field.
var titleText = ""
override func viewDidLoad() {
super.viewDidLoad()
label.text = titleText
}
Note you cannot assign directly to your label in prepareForSegue because the label is not guaranteed to have been initialized until ViewDidLoad has been called. Thats why the value goes into a property. Proper model view separation also dictates that one controller shouldn't be writing to another controller's view anyways.
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 7 years ago.
How can I pass data between from tableView to another tableView? As this example.
You need to use prepareForSegue and over there you will have the opportunity to pass data to your destination view controller.
Basically you need to set a segue identifier at the connection you made for the second view controller at the storyboard and then you will override (at the first view controller) the method prepareForSegue.
This method will be called automatically by the framework when ever the user will tap the relevant button/cell.
This method initiate your second view controller before the user will see the actual second view controller. You will then have the opportunity to pass/set what ever data you want from the first to the second view controller.
// This will get called before the destination view controller appears
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
{
if (segue.identifier == "IdentifierNameYouSet") {
// Get destination view
if let destinationVc = segue.destinationViewController as? YourTableViewControllerName{
// Pass the information to your destination view
destinationVc.propertyArray = yourOriginateVcArray
}
}
}
I've been trying to pass data to another View Controller. But as I have two Bar Buttons that lead to two different View Controllers I have to set a condition. But when I try to pass the data it won't work. Here's the code I've been using:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ViewController") {
let destViewController = segue.destinationViewController as! ViewController
destViewController.chips = chips
destViewController.blind = blind
}
}
I have my destination Storyboard id to ViewController but when ViewController view opens the data isn't passed through.
EDIT:
In both View Controllers chips and blind are declared as:
var chips = 0
var blind = 0
Before I added a back button the data was passed correctly. But then the application crashed every time I clicked "Back" so I decided to add a condition which doesn't seem to work.
I'm very new to Xcode/Swift, but I believe the string in your if (segue.identifier == "ViewController") is the problem. Instead of "ViewController" you need to use the identifier of your segue. Select your segue in Main.storyboard, click on the Attributes Inspector and give it a name in the Identifier field. That's the string you want to use.