Swift中页面跳转的三种方式

第一种,很简单,直来直往

//跳转
self.presentViewController  
//返回
self.dismissModalViewController 

第二种,必须在当前视图有navigation,要不实现不了

self.navigationController?.pushViewController  
self.navigationController?.popViewController 

示例代码

let myStoryBoard = self.storyboard     
let loginView:UIViewController = myStoryBoard?.instantiateViewControllerWithIdentifier("login") as UIViewController                   
//login是视图的storyboard id    
self.navigationController?.pushViewController(loginView, animated: true)    

第三种 通过ViewDelegate实现
在prepareForSegue中传递参数, 使用performSegueWithIdentifier跳转, 在shouldPerformSegueWithIdentifier 中判断是否应该跳转

// 在这个方法中给新页面传递参数
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier != "userInfoSegue" {
            let segueName = segue.identifier
            // 选中的行
            let selectedRowIndex = self.personalInfoTableView.indexPathForSelectedRow()?.row
            let controller = segue.destinationViewController as! UIViewController // 有多个view, 都是UIViewController的子类
            // 取选中行的labelname, 设置目标controller的title
            controller.title = lableNamesOfCersonalInfoCells[selectedRowIndex!]
        }
    }
2015-06-28 15:172