Reputation: 1035
I have a submit UIButton that is targeted at this function and I have a textField that I want to pass onto the next view controller. So the user would type into the textField and whatever it is typed, once the user taps on the submit button, the string inside the text field will be passed onto the next view controller's UILabel.
func submitTapped() {
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
navigationController?.pushViewController(secondViewController, animated: true)
print("Button tapped")
}
}
How do I push that string though the second controller? Also, I have tried using prepareForSegue, but I think this has nothing to do with segues as we are not transitioning screen via a segue, we are transitioning via a navigation controller, am I right?
thanks!
Upvotes: 0
Views: 1846
Reputation: 1589
In YourSecondViewController, Declare a variable.
class YourSecondViewController {
var strData: String?
}
Pass data using below code:
func submitTapped() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let objSecond = storyboard.instantiateViewController(withIdentifier: "YourSecondViewControllerID")
objSecond.strData = yourTextField.text!
self.navigationController?.pushViewController(objSecond, animated: true)
}
Upvotes: 1
Reputation: 2842
In SecondViewController, Create a variable and assign value while you create an object. Check example below:
class SecondViewController {
var dataString: String?
}
Pass data like below:
func submitTapped() {
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
secondViewController.dataString = textField.text!
navigationController?.pushViewController(secondViewController, animated: true)
print("Button tapped")
}
}
Upvotes: 3