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.
Related
The problem is label.text value became nil every time all the time accept in viewload. I do not know what is the issue.
I have this code in swift 3 Xcode 8, I have a label as shown below
Label to be set later
then I want to assign the label a value when the view loads as initial value
This is once view loads and it is working fine :)
later on I want to change the label value to the current Date which I would like it to be set when the user moves to another view the comes back , so I made the function open as you say ..
Here is how I call the function
function call from secondViewController
finally I want to set it to those values
last desired values
Do this in your firstviewcontroller ,define a property say :
var x : String?
Now in your secondview controller do this :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let firstviewcontroller = segue.destination as! FirstViewController
firstviewcontroller.x = "sample value"
// sample value will be displayed on the firstview controller when u move from secondviewcontroller to firstviewcontroller on the label
}
Now in firstviewcontroller set the label in the viewDidLoad() method:
dateItem?.text = x
Note: No need to create another instance of FirstViewController in prepare function because the inbuilt function of UIViewController override func prepare(for segue: UIStoryboardSegue, sender: Any?) is responsible for going from one view controller to other , you just have to compare the identifier
The problem lies in prepare for segue. When you have got your reference using segue.destination to the firstViewController, you don't need to do
s = FirstViewController(), by this you are creating a new instance and not using the one which will actually get initialised during the segue, use firstController.calcPray()
I am looking for help in figuring out how to have a row in a MultivaluedSection present a view controller with a second Eureka form and return a value back to the MultivaluedSection row. I've been able to get a regular ButtonRow to push a view controller using a segue, but I can't figure out not to get a value back to the row in the MultivaluedSection. I'm not sure if the ButtonRow method supports returning values or not so I started looking for other solutions. One I found is to use a custom presenter row (https://github.com/xmartlabs/Eureka#custom-presenter-rows), but I don't understand how to make that work.
Here one thing I did find, but again, I don't understand how to put this all together:
Help creating simple Custom Presenter Row
- https://github.com/xmartlabs/Eureka/issues/716
Can someone either point me to a working sample or help walk me through getting this setup?
If you are already pushing a new VC with a segue, then you might want to implement a protocol and define the functions to pass data back.
Here is a good tutorial with Navigation controllers where at the end a Protocol is added.
eg:
View one (could be the Form View Controller with the ButtonRow)
class FormVC: FormViewController , FooViewControllerDelegate{
var text : String!
override func viewDidLoad() {
super.viewDidLoad()
}
/// Delegate protocol callback implementation
func myVCDidFinish(controller: FooViewController, text: String) {
// Receive the data as a delegate
self.text = text
// In this case we also want to finish the view
controller.navigationController?.popViewController(animated: true)
}
/// This represents the prepare for segue mentioned as implemented in the question
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Act upon the segue we want from this VC
// The string is defined in the storyboard, so it must be exactly the same
if segue.identifier == "mySegue"{
// Creating the second VC instance
let vc = segue.destination as! FooViewController
// Since this class is now a delegate, setup the delegate
vc.delegate = self
}
}
}
View two (the pushed View controller)
protocol FooViewControllerDelegate {
func myVCDidFinish(controller: FooViewController, text: String)
}
class FooViewController: UIViewController {
/// Data
var text : String!
/// Set up an optional delegate
var delegate:FooViewControllerDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Init label
self.text = "Pushed view data to pass back
}
}
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...
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I'm studying for the iOS Developer Nanodegree from Udacity and I can't figure out how to pass information between view controllers.
In this video, the instructor ask that we present view controllers using code, segue & code, and segue.
https://www.youtube.com/watch?v=zGzu5PcP8TI
I spent hours trying to understand this but I'm not getting anywhere. Is there any resources that clearly explains this?
Let's say you have VC1 and VC2 (two UIViewController classes) and that they are both in the same Storyboard file.
VC1 and VC2 both have a property:
var aProperty: String
VC1 has a UITextField, and when it changes, you set aProperty to the text field's text.
Now, you want to show VC2 and VC2 needs the value of aProperty from VC1.
If you set up a segue from VC1 to VC2, when it is triggered, this function in VC1 will be called (if it exists)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// You can set up VC2 here
}
For example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc2 = sender as? VC2 {
vc2.aProperty = vc1.aProperty
}
}
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
}
}
}