IOS學習 ARC下一些系統調用對對象的retainCount的影響

首先在非ARC下:

testButton *_tempBtn = [[testButton alloc]initWithFrame:CGRectMake(0, 200, 200, 200)];

[self.view addSubview:_tempBtn];

//此時_tempBtn的引用計數爲2



然後在ARC下:

testButton *_tempBtn = [[testButton alloc]initWithFrame:CGRectMake(0200200200)];

[self.view addSubview:_tempBtn];

//此時_tempBtn的引用計數爲2

[_tempBtn addTarget:self action:@selector(CPCarPoolingFeeVC:paidSuccessfullyOrder:) forControlEvents:UIControlEventTouchUpInside];

//addTarget不會增加_tempBtn的retainCount(此時還是2),addTarget也不會增加self的引用計數

//addTarget是將target加入_tempBtn對象的NSSet(推測根據:allTarget函數)中,所以正常也不會增加_tempBtn的retainCount。

[[NSNotificationCenter defaultCenter] addObserver:_tempBtn selector:@selector(applicationLaunchedWithUrl:) name:@"asdasda" object:nil];

//addObserver不會增加_tempBtn的retainCount(此時還是2)


當self這個Controller被釋放的時候會先走controller的dealloc此時_tempBtn存在,在這之後會走_tempBtn的dealloc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章