代码 + storyBoard拖拽法,实现手动切换视图

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)
    
}
相关文章
相关标签/搜索