本文介绍了uiviewcontroller解除分配时如何在目标c中触发阻止事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在UIViewController取消分配时在目标C中触发阻止事件.
How to fire block event in Objective C when UIViewController dealloc.
例如:
[PGMemberObj requestWithUserName:@"ab" andPassword:@"cc" andCallback:^(BOOL isSuc){
if (isSuc) {
NSLog("Login Suc.");
}else
{
NSLog("Login Failed");
}
}];
当我弹出ViewController并执行dealloc时,我仍然收到Login Suc.或登录失败消息.如何避免这个问题?
when i pop ViewController and dealloc was executed,i still receive Login Suc. or Login Failed Message.How to avoid this issue?
推荐答案
尝试以下代码:
__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
}];
它将测试您的视图控制器是否仍然存在并且仍在屏幕上.只需检查weakSelf
,如果您不在乎它是否仍在屏幕上显示.
It will test whether your view controller still exists AND is still on screen.Just check for weakSelf
if you don't care if it's still being shown on screen.
if (weakSelf)
//Still exists
else
//dealloc'd
这篇关于uiviewcontroller解除分配时如何在目标c中触发阻止事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!