0

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..

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.

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)

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

@property (strong, nonatomic) LKNetworking *strongNetworkingProperty

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

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..

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!

Thanks

JerseyDevel
  • 1,334
  • 1
  • 15
  • 34

1 Answers1

6

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848