我的 firstViewController 上有一个文本字段,我想要发生的是:一旦用户输入文本并转到 SecondViewController,它将成为导航栏标题。
我是编程新手,我希望有人可以帮助我。
导航创建视图控制器对象并通过导航控制器导航的第一种方法
let second = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
second.title = textTitle.text!
navigationController?.pushViewController(second, animated: true)
从情节提要导航的第二种方式
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "segueIdentifier" else { return }
let destination = segue.destination as? SecondViewController
destination?.strTitle = textTitle.text!
}
在第二个视图控制器中添加这个
var strTitle : String?
override func viewDidLoad() {
super.viewDidLoad()
self.title = strTitle ?? "default string"
}
您可以尝试@ChanWarde 的方式但呈现视图控制器而不是使用 segues 吗?
let second = storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
second.title = textTitle.text!
present(second, animated: true, completion: nil)
或尝试:
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)
})