MRC内存管理(二)数组、控件

续上一篇MRC内存管理(一)

- (void)viewDidLoad {
    [super viewDidLoad];
    NSObject *obj = [[NSObject alloc] init];
    
    NSLog(@"对象--%lu",(unsigned long)obj.retainCount);
    
        //当把对象放入数组中时,数组会对这个对象引用计数+1.
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:obj, nil];
    NSLog(@"加入数组对象--%lu",(unsigned long)obj.retainCount);
    
        //把对象从数组中移除时,对象的引用计数-1.
    [array removeObject:obj];
    NSLog(@"移除数组对象--%lu",(unsigned long)obj.retainCount);
        //当数组本身被释放前,会对自己内部所有的对象release一次。
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 200, 40)];
    label.text = @"123";
    NSLog(@"控件对象--%lu",(unsigned long)label.retainCount);
    
        //当把视图添加到父视图时,父视图会对这个视图retain一次。
    [self.view addSubview:label];
    NSLog(@"加入父视图--%lu",(unsigned long)label.retainCount);
    
        //当视图从父视图移除时,引用计数-1
        //[label removeFromSuperview];
    
        //当视图释放前,会对自身所有的子视图release一次
    [label release];
}

- (IBAction)showAlertClick:(UIButton *)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:@"消息" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    NSLog(@"alert--%lu",(unsigned long)alert.retainCount);
        //show会导致alert的引用计数增加。
    [alert show];
    NSLog(@"show--%lu",(unsigned long)alert.retainCount);
    [alert release];
}

- (IBAction)presentViewController:(UIButton *)sender {
    SecondVC *vc = [[SecondVC alloc] init];
    NSLog(@"创建VC--%lu",(unsigned long)vc.retainCount);
        //presentViewController模态弹出
        //模态弹出会导致vc引用计数增加
    [self presentViewController:vc animated:YES completion:nil];
    NSLog(@"模态弹出--%lu",(unsigned long)vc.retainCount);
    [vc release];
}
输出结果如下:

MRC内存管理(二)数组、控件_第1张图片

至于一个模态弹出为何输出引用计数这么大,博主也甚是不解,望广大友人给予帮忙解释一下,感激不尽,谢谢!

你可能感兴趣的:(内存管理)