时机

2016-08-23  本文已影响0人  潜水100号

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
static var i = 0
//1.
//通过代码创建对象时调用
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

    print("ViewController::init(nibName:bundle:)")
}

//通过Storyboard创建对象时自动调用
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    print("ViewController::init(coder:)")
}

//[2.]
//Storyboard中创建的,如果代码创建不被调用
override func awakeFromNib() {
    super.awakeFromNib()
    
    print("ViewController:awakeFromNib()")
}

[3.]
//加载self.view
//唯一一个可以更换self.view的位置
//比需要在里面创建self.view

// override func loadView() {
// print("ViewController:loadView()")
//
// let v = UIView()
// v.backgroundColor = UIColor.greenColor()
// self.view = v
// }

//4.
//self.view已经被创建
override func viewDidLoad() {
    super.viewDidLoad()
    
    print("ViewController::viewDidLoad()")
}

//5. 页面将要显示
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    
    print("ViewController::viewWillAppear(:)")
    label.text = "\(ViewController.i)"
}

//6. 将要开始布局
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    
    print("ViewController::viewWillLayoutSubviews()")
}

//7. 布局完成
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    //可以获取自动布局控件的真实位置和大小
    print("ViewController::viewDidLayoutSubviews()")
}

//8. 页面已经显示
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    print("ViewController::viewDidAppear(:)")
}
//9.页面将要消失
override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    
    print("ViewController::viewWillDisappear(:)")
}

   //10.
    //在 storboard跳转到新的界面时调用  都是界面创建界面的时候
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    print("prepareForSegue:storyboard")
    let second = segue.destinationViewController as! SecondViewController
   // second.label = nil //这时候变量是能够拿到的
  //  second.label.text = "123" 变量有并不代表对象存在,因为这时候第二个界面根本就没有加载 要等到vidload才加载 所以时机不对
    second.c = "123" //变量是能够拿到的,用变量赋值就ok了
    
    //最蠢得办法

// let secondCtrl = segue.destinationViewController as! SecondViewController
//
//self.view:懒加载,第一次使用时加载
//不要强制加载self.view
// secondCtrl.view.backgroundColor = UIColor.blueColor()

    //1. 获取到第二个页面的“对象”
    //2. 获取到第二个页面上的UILabel的“对象”
    //            secondCtrl.label.text = "xxx"

}

//11.
override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    
    print("ViewController:viewDidDisappear(:)")
}

//12.
deinit {
print("deinit")
}
}

上一篇下一篇

猜你喜欢

热点阅读