在轉跳頁面以前咱們可能會判斷一下是否有輸入正確的數據,以下圖所示,若是沒有數據輸入則彈出提示,不進行轉跳。ide
1.第一個頁面的上放上一個Label一個textfield和一個按鈕spa
2.在第二個窗體上放兩個label和一個按鈕,並指定一個ViewControllercode
3.建立Segue鏈接兩個窗體,打開模式選擇modal,並給Segue命名爲"openView"orm
3.頁面1 中建立輸入文本框的引用和按鈕的點擊Actionblog
在Action裏判斷文本是否有數據,若是沒有則彈出提示,若是有則打開新窗體ip
@IBAction func passNameToView(sender: AnyObject) { guard let text = teName.text where !text.isEmpty else { let title = "Warning" let message = "please enter name!" let okText = "Ok" let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) let okButton = UIAlertAction(title: okText, style: .Cancel, handler: nil) alert.addAction(okButton) presentViewController(alert, animated: true, completion: nil) return; } self.performSegueWithIdentifier("openView", sender: self) // or if let text = teName.text where !text.isEmpty }
這樣就能夠打開新窗體了。字符串
4.重新窗體返回原窗體get
頁面2中的按鈕創建一個Action代碼以下it
@IBAction func backToParent(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) }
1.在頁面2中聲明一個字符串變量用於接收傳過來的信息和一個lable的引用,在加載完窗體後顯示在label中io
@IBOutlet weak var receiveName: UILabel! var receivedStr: String = "" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. receiveName.text = receivedStr }
2.頁面1 重寫prepareForSegue方法 把值輸入的值傳遞給頁面2
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "openView" { if let secondView = segue.destinationViewController as? SecondViewController { secondView.receivedStr = teName.text! } } }
ok整個過程完畢。
完整代碼:判斷條件頁面轉跳.zip