1。 代理 weak解决 循环引用
2 ,block 实例变量
3, 注册观察着,要在delloc 中注销 观察着
4,timer
刚调试程序时发现一个很诡异的问题,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的delloc(),很诡异的问题,因为按理说此时点击back是执行pop操作的,是会执行delloc()函数的,但经调试发现确实没有执行。
后来经过google发现,
The dealloc method was not being called if any of the references held by a viewcontroller were still in memory. |
timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
将定时器注销掉 , 对象的引用计数 为0 方可走 delloc
NSTimer
@interface ForgetPasswordViewController ()
{
NSTimer *timer;
int count;
}
count = 60;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateBtnState) userInfo:nil repeats:YES];
-(void)updateBtnState
{
if (count == 0) {
if (timer) {
[timer invalidate];
timer = nil;
}
self.timeLabel.text = @"获取验证码";
self.getCodeBtn.enabled = YES;
self.codeStr = nil;
}else
{
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI操作
self.timeLabel.text = [NSString stringWithFormat:@"%d%@",count,@"秒"];
// [self.getCodeBtn setTitle:@"" forState:UIControlStateNormal];
self.getCodeBtn.enabled = NO;
count--;
});
}
}
- (IBAction)backBtn:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
if (timer) {
[timer invalidate];
}
}
-delloc{} 才会走
GCD
@implementation RegisterViewController
{
dispatch_source_t _timer;
dispatch_source_t _distimer;
}
__block int timeout = 60;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(_timer, ^{
if (timeout <= 0) {
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
self.mGetCodeBtn.hidden = NO;
self.mTimeLabel.text = @"Get code";
[self.mGetCodeBtn setTitle:@"Get code" forState:UIControlStateNormal];
[self.mGetCodeBtn setBackgroundColor:[UIColor colorWithHex:@"#D62961"]];
[self.mGetCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
});
}else
{
NSString *strtime = [NSString stringWithFormat:@"%.2d",timeout];
dispatch_async(dispatch_get_main_queue(), ^{
self.mGetCodeBtn.hidden = YES;
self.mTimeLabel.text = [NSString stringWithFormat:@"%@s重新发送",strtime];
});
timeout -- ;
}
});
dispatch_resume(_timer);
}
- (IBAction)backAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
if (timer)
{
timer = nil
}
}