1

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 View controller

Second View controller

Chanchal Warde
  • 983
  • 5
  • 17

2 Answers2

1

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"
}
Chanchal Warde
  • 983
  • 5
  • 17
  • I put this in tableViewDidSelectRow: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { myBudget = [indexPath.row] // performSegue(withIdentifier: "segue", sender: self) func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "segue" else { return } let destination = segue.destination as? SecondViewController destination?.strTitle = myBudgetTitle.text! } } -but it won't let me go to secondViewController –  May 04 '18 at 07:12
  • did you added push segue. and put perform segue in the view controller. I cant "override" in your code. You have to override this function in your first view controller. – Chanchal Warde May 04 '18 at 07:35
  • I think you should follow this tutorial https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ – Chanchal Warde May 04 '18 at 07:36
0

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)
})
PhillipJacobs
  • 2,337
  • 1
  • 16
  • 32
  • I did it. my fault. I forgot to fill up the storyboard id. the view controller now is being presented. but the navigation bar in the secondViewController disappeared. why is that so? –  May 04 '18 at 09:04
  • Which code are you using to display the secondViewController? – PhillipJacobs May 06 '18 at 05:09
  • func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { myBudget = [indexPath.row] let second = storyboard?.instantiateViewController(withIdentifier: "segue") as! SecondViewController second.strTitle = myBudgetTitle.text! present(second, animated: true, completion: nil) } –  May 08 '18 at 01:26
  • and at the SecondViewController. I used self.navigationItem.title = strTitle ?? "default string" in viewdidload –  May 08 '18 at 01:28
  • If you're using a navigation controller then you'll have to follow @ChanWarde code. If you have your own custom navbars in your viewControllers then presenting won't be a problem. – PhillipJacobs May 08 '18 at 08:41