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

How to perform delayed selector safely iOS

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

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

viewDidDisappear

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