当 UIViewController dealloc 时,如何在 Objective C 中触发 block 事件。
例如:
[PGMemberObj requestWithUserName:@"ab" andPassword:@"cc" andCallback:^(BOOL isSuc){
if (isSuc) {
NSLog("Login Suc.");
}else
{
NSLog("Login Failed");
}
}];
当我弹出 ViewController 并执行 dealloc 时,我仍然收到 Login Suc。或登录失败消息。 如何避免这个问题?
最佳答案
试试下面的代码:
__weak UIViewController *weakSelf = self;
[PGMemberObj requestWithUserName:@"ab" andPassword:@"cc" andCallback:^(BOOL isSuc){
if ([weakSelf isViewLoaded] && [weakSelf.view window])
//The view controller still exists AND it's being shown on screen
else
//Either dealloc'd or not on screen anymore
}];
它将测试您的 View Controller 是否仍然存在并且仍在屏幕上。
如果您不关心它是否仍在屏幕上显示,只需检查 weakSelf
。
if (weakSelf)
//Still exists
else
//dealloc'd
关于ios - uiviewcontroller dealloc时如何在 objective-c 中触发 block 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512400/