如何安全地执行延迟选择器iOS

时间:2018-06-07 23:36:16

标签: ios objective-c selector nstimer

viewDidAppear中,我会在3秒后向用户显示弹出窗口。如果用户在计时器开始后导航到另一个viewController,该怎么办?所选功能将尝试执行&当superview不再在屏幕上时显示弹出窗口。应用程序不会崩溃或抛出任何错误,但我想确认这是安全的。我应该在选择器方法中设置BOOL并断言isCurrentViewYES吗?

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self performSelector:@selector(showPopup) withObject:nil afterDelay:2.5];
}

1 个答案:

答案 0 :(得分:1)

viewDidDisappear

中的

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:(BOOL)animated];       
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showPopup) object:nil];
}