利用 prepare 這個function
call 了 performSegue(withIdentifier: "gotoview2", sender: inputtext)
@IBAction func changeView(_ sender: UIButton) {
//check if user input text or not
if let inputtext = myTextInput.text{
if inputtext == ""{
// no input, pop up an alert
let myalert = UIAlertController(title: "No input", message: "Please enter something", preferredStyle: .alert)
let okaction = UIAlertAction(title: "OK", style: .default, handler: nil)
myalert.addAction(okaction)
present(myalert, animated: true, completion: nil)
}else{
performSegue(withIdentifier: "gotoview2", sender: inputtext)
}
}
}
然後系統就會自動呼叫 prepare()... 在這裡面判斷要去的 viewcontroll
在這裡我們要去 gotoview2 這個 viewcontroller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "gotoview2"{
if let lightred = segue.destination as? lightRedViewController{
lightred.infoFromView1 = sender as? String
}
}
}
把數值存到 gotoview2 這個 viewcontroller 的變數 infoFromView1
-------------------------------
class lightRedViewController: UIViewController {
var infoFromView1:String?
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.text = infoFromView1
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
-------------
回到剛剛的viewcontroll...
前提是有embed navigatorcontroller
--------------
-------------
回到剛剛的viewcontroll...
let _ = navigationController?.popViewController(animated: true)
前提是有embed navigatorcontroller
--------------
myTextInput.resignFirstResponder() // release the focus
沒有留言:
張貼留言