UIAlertViewDelegate类“自我”实例在按下按钮之前得到dealloc'd [英] UIAlertViewDelegate class "self" instance gets dealloc'd before button gets pressed

查看:72
本文介绍了UIAlertViewDelegate类“自我”实例在按下按钮之前得到dealloc'd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的解决方案几天,并且发现了一些类似的帖子,但似乎没有人发布一个明确的答案,如何阻止我正在使用的实例获得dealloc'd 。希望我只是错过了,或者有人可以帮助我停止摧毁我的大脑..

I have been searching for a solution to this issue for a few days and have found some similar posts, but no one seems to have posted a definitive answer to how to stop the instance i'm working with from getting dealloc'd. hopefully I just missed it, or someone can help me stop wracking my brain..

我有一个班级( LKNetworking )声明它符合 UIAlertViewDelegate 协议。目前它是 NSObject 的子类,但是我已经尝试了 UIResponder 但是产生了同样的问题。

I have a class (LKNetworking) which is declaring that it conforms to the UIAlertViewDelegate protocol. Currently it is a subclass of NSObject, however I have tried UIResponder but either produces the same issue.

问题是我创建了一个UIAlert视图并将委托设置为 self 。 UIAlert视图显示OK,但是调用它的 LKNetworking 类实例立即被释放(如我在NSLOG中所示),当我单击按钮时,消息得到发送到解除分配的实例(LKNetworking alertView:clickedButtonAtIndex:]:发送到解除分配的实例0x6e5ce40的消息)

Issue is that I create a UIAlert view and set the delegate to self. the UIAlert view shows OK, but then the LKNetworking Class instance that called it gets instantly dealloc'd (as I show in NSLOG) and when I click a button, the message gets sent to a deallocated instance (LKNetworking alertView:clickedButtonAtIndex:]: message sent to deallocated instance 0x6e5ce40)

所以,我试图在.h文件中创建一个属性as例如:

SO, I have tried to create a property in the .h file as such:

@property (strong, nonatomic) LKNetworking *strongNetworkingProperty

然后我尝试将我的UIAlertView的委托设置为strongNetworkingProperty ivar,消息似乎无处可去,所以没有好处..

I then try to set my UIAlertView's delegate to the strongNetworkingProperty ivar and the message seems to go nowhere, so thats no good..

我看到一些答案,人们说将警报视图委托设置为 nil ,但是由于我需要根据用户的选择做出相应的行动,因此打败了我的整个目的..

I saw some answers where people say to set the alert views delegate to nil, but that defeats my entire purpose since I need to act accordingly in response to the users selection..

这里是 LKNetworking 调用警报的方法委托方法:

here is the sample code of the LKNetworking method where the alert is called and the delegate method:

-(void)checkGameServerVersion: (float) serverGameVersion {
    if (serverGameVersion == GAMEVER) {
        NSLog(@"matched");
        // Continue flow here..
    } else {
        NSLog(@"not matched");
        //Game version does not match, end game, force upgrade 
        UIAlertView *oldVersion = [[UIAlertView alloc]initWithTitle:@"Ugrade Required" message:@"A new game version is available, Please update your game and try again." delegate:self cancelButtonTitle:@"Quit" otherButtonTitles: nil];
        [oldVersion show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"button = %d",buttonIndex);
    if (buttonIndex == 0 ) {
        NSLog(@"pressed button 0, quitting gam");
    }
}
@end

任何建议都会很大赞赏!

Any suggestions would be greatly appreciated!

谢谢

推荐答案

如果你想让你的LKNetworking留下来一些东西需要保持对它的引用。那个东西可以是LKNetworking对象本身。创建警报视图时设置strongNetworkingProperty = self。这将创建一个保留周期,以防止系统取消分配LKNetworking对象。然后在alertView:clickedButtonAtIndex:中,将strongNetworkingProperty设置为nil以打破保留周期。

If you want your LKNetworking to stay around, something needs to keep a reference to it. That something can be the LKNetworking object itself. Set strongNetworkingProperty = self when you create the alert view. This creates a retain cycle that prevents the system from deallocating the LKNetworking object. Then in alertView:clickedButtonAtIndex:, set strongNetworkingProperty to nil to break the retain cycle.

这篇关于UIAlertViewDelegate类“自我”实例在按下按钮之前得到dealloc'd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆