关于IBOutlet的生命周期

在调试程序的时候,发现 IBOutlet的对象在执行 getter时,开始一直想找IBOutlet对象getter方法前被执行的代码,但是一直找不到,于是我就想是不是系统自动给IBOutlet 自动初始化了,于是做了一个实验,得出一个结论IBOutlet对象getter方法 会在awakeNib之后但在viewDidload后进行初始化。

当上一个视图 Segue 传递过来的一个变量,那么是先执行 -(void) awakeFromNib 然后在执行变量的setter方法

- (void)viewDidLoad
{
    NSLog(@"ViewDidLoad!");
    [super viewDidLoad];
    [self.scrollView addSubview:self.imageView];
}

- (void) awakeFromNib{
    NSLog(@"awakeFromNib");
    self.splitViewController.delegate = self;
}

-(void)setSpinner:(UIActivityIndicatorView *)spinner{
    NSLog(@"IBAction Spiner");
}

- (void)setScrollView:(UIScrollView *)scrollView
{
    NSLog(@"IBAction ScrollView");
    _scrollView = scrollView;
    
    // next three lines are necessary for zooming
    _scrollView.minimumZoomScale = 0.2;
    _scrollView.maximumZoomScale = 2.0;
    _scrollView.delegate = self;

    // next line is necessary in case self.image gets set before self.scrollView does
    // for example, prepareForSegue:sender: is called before outlet-setting phase
    self.scrollView.contentSize = self.image ? self.image.size : CGSizeZero;
}



//output:
2014-11-06 17:11:13.216 Photomania[760:60b] awakeFromNib
2014-11-06 17:11:21.891 Photomania[760:60b] IBAction ScrollView
2014-11-06 17:11:21.892 Photomania[760:60b] IBAction Spiner
2014-11-06 17:11:21.895 Photomania[760:60b] ViewDidLoad!

 

posted @ 2014-11-06 17:14  南望青天  阅读(254)  评论(0编辑  收藏  举报