主动释放视图

arc下,给对象赋值nil,对象内存是否释放?测试下1、从UIView派生一个子类TestView,重写delloc方法,在其中打印“对象释放”。- (void)dealloc{    NSLog(@"对象释放");}2、在页面中,给对象赋值nil,看它是否调用delloc方法。#import "ViewController.h"#import "TestView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        TestView *testView = [[TestView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];    testView.tag = 1111;    [self.view addSubview:testView];    }- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

TestView *testView = (TestView *)[self.view viewWithTag:1111];

[testView removeFromSuperview];

testView = nil;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

结果:

1、写这两句

[testView removeFromSuperview];

testView = nil;

打印:


参考

http://blog.csdn.net/u013513053/article/details/48549741

http://blog.csdn.net/xunyn/article/details/8283653

2、只写上一句

无打印

参考

http://blog.csdn.net/yidu_blog/article/details/51453148

https://segmentfault.com/q/1010000007099186/a-1020000007103144

3、只写下一句

无打印。原因可能是,视图还在视图树中,相当于对象还在容器中,赋值nil时,realse一次,引用计数还没归零。

你可能感兴趣的:(主动释放视图)