兩個界面的連接

ViewController

    @IBAction func didClicked(sender: UIButton) {
        //1. 創建一個頁面對象
        let secondCtrl = SecondViewController()
        //2.找到一個已經顯示的頁面
        //模態視圖Modal
        //對于正在顯示的頁面或控件,系統會自動維持它的強引用
//        self.presentViewController(secondCtrl, animated: true, completion: nil)
        
        //1. 獲取window
//        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
//        appDelegate.window?.rootViewController = secondCtrl
        
        //2. 獲取window
//        UIApplication.sharedApplication().keyWindow?.rootViewController = secondCtrl
        
        //3. 對于一個已經顯示的視圖,一定有一個window屬性
        self.view.window?.rootViewController = secondCtrl
    }

    deinit {
        print("第一個頁面銷毀")
    }

SecondViewController

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    //每一個頁面在顯示之前都會調用viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.redColor()
        
        let tableView = UITableView(frame: self.view.bounds, style: .Plain)
        tableView.dataSource = self
        tableView.delegate = self
        //顯示一個控件,會自動提供強引用
        self.view.addSubview(tableView)
        
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = "aaa"
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //讓當前頁面消失
        //系統自動將提供的強引用刪除
//        self.dismissViewControllerAnimated(true, completion: nil)
        
//        let firstCtrl = ViewController()
        
        //從Storyboard獲取新的第一個頁面
        let firstCtrl = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        
        self.view.window?.rootViewController = firstCtrl
    }
    
    deinit {
        print("通知該對象即將銷毀")
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容