ViewController
@IBAction func didClicked(sender: UIButton) {
//1. 創(chuàng)建一個頁面對象
let secondCtrl = SecondViewController()
//2.找到一個已經(jīng)顯示的頁面
//模態(tài)視圖Modal
//對于正在顯示的頁面或控件,系統(tǒng)會自動維持它的強引用
// 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. 對于一個已經(jīng)顯示的視圖,一定有一個window屬性
self.view.window?.rootViewController = secondCtrl
}
deinit {
print("第一個頁面銷毀")
}
SecondViewController
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//每一個頁面在顯示之前都會調(diào)用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) {
//讓當前頁面消失
//系統(tǒng)自動將提供的強引用刪除
// self.dismissViewControllerAnimated(true, completion: nil)
// let firstCtrl = ViewController()
//從Storyboard獲取新的第一個頁面
let firstCtrl = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
self.view.window?.rootViewController = firstCtrl
}
deinit {
print("通知該對象即將銷毀")
}
}