1.準備:swift
在storyBoard(故事板)中使用拖拽法,將起始視圖 和 目標視圖鏈接起來,而後給他們之間的「鏈接線」(稱爲segue)添加 identifier。ide
2.代碼實現手動跳轉:函數
前提:衆所周知,用」拖拽法「實現視圖間的切換默認是」觸發當即跳轉「的。可是,有時候咱們會有這樣的需求,咱們須要作一些判斷,返回 真 即跳轉,返回 假 就提示錯誤信息。這個時候,咱們就須要手動切換視圖,禁止」觸發當即跳轉「。code
2.1 禁止segue的默認跳轉orm
//根據segue的identifier禁止默認切換,這裏的identifier就是 1 中添加的segue的identifier override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool { return identifier == "toListView" ? false : true }
2.2 手動切換目標視圖ip
//MARK: - IBAction @IBAction func loginAction(sender: UIButton) { let username = self.username.text let password = self.password.text self.loading.startAnimating() if( username == "" || password == "" ){ self.loading.stopAnimating() self.alertTipAction("登陸Issac-Note", message: "請輸入完整信息", confirm: "肯定") }else { loginCheck(username, pwd: password) //這裏使用了定時器,能夠不使用 NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(segueToListView), userInfo: nil, repeats: false) } } //手動切換到目標視圖 func segueToListView(){ self.loading.stopAnimating() if self.segueToView { //這是手動切換的關鍵,傳入連結目標視圖的segue的identifier,並執行這個函數就會切換到目標視圖 performSegueWithIdentifier("toListView", sender: nil) }else{ self.alertTipAction("登陸Issac-Note", message: "密碼或帳號有誤", confirm: "肯定") } } //提示模態框 func alertTipAction(title: String, message: String, confirm: String){ let alertAction = UIAlertController(title: title, message: message, preferredStyle: .Alert) let cancelAction = UIAlertAction(title: confirm, style: .Cancel, handler: { sender in }) alertAction.addAction(cancelAction) self.presentViewController(alertAction, animated: true, completion: nil) }