I have a textfield on my firstViewController and what I want to happen is: once the user entered a text and goes to the SecondViewController it will become the navigation bar title.
I'm new at programming and I was hoping someone could help me.
I have a textfield on my firstViewController and what I want to happen is: once the user entered a text and goes to the SecondViewController it will become the navigation bar title.
I'm new at programming and I was hoping someone could help me.
First way to navigate creating object of view controller and navigate through navigation controller
let second = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
second.title = textTitle.text!
navigationController?.pushViewController(second, animated: true)
Second way to navigate from storyboard
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "segueIdentifier" else { return }
let destination = segue.destination as? SecondViewController
destination?.strTitle = textTitle.text!
}
In second view controller add this
var strTitle : String?
override func viewDidLoad() {
super.viewDidLoad()
self.title = strTitle ?? "default string"
}
Can you try @ChanWarde way but present the view controller rather than using segues ?
let second = storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
second.title = textTitle.text!
present(second, animated: true, completion: nil)
Or try:
let second = storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
second.title = textTitle.text!
DispatchQueue.main.async(execute: {
UIApplication.shared.keyWindow?.rootViewController = second()
self.dismiss(animated: true, completion: nil)
})