2

I've got an issue where an object that's creating a UIAlertView is sending a message back to its delegate, but the delegate object is crashing.

I've got it working OK in a couple of other instances, but what I'm trying to do in this case (and what's different from other similar questions) is that the object that instantiates the alert, and which acts as its delegate is not itself a view, but rather an object that is instantiated within a view. To wit, in the parent view:

@implementation
*MyCustomObject customObject;

-(void)viewDidLoad {
  customObject = [[MyCustomObject alloc] init];
}
@end

And in the custom object:

-(void)DoCoolThings {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Do You Want To Do Cool Things"
                                                  message:@"...description of cool things..."
                                                 delegate:self
                                        cancelButtonTitle:@"No Thanks"
                                        otherButtonTitles:@"HELLS YES", nil];
    [message show];
}

and

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex == 1) {
    [self DoCoolThings];
   } else {
    [self GoAndCry];
   }
}

When I do the alert view within a viewcontroller object, everything is fine. Doing it within this sub-object which itself has no view allows me to create the alert, but that object--which shouldn't be getting de-allocated based on the scoping--doesn't seem to want to continue to act as a delegate.

The error I'm getting is indeed a de-allocation message, but I feel strongly that this is not the problem because if I remove the alert, all the other stuff--specifically, it's a wrapper for a storekit purchase process--works fine, and all those delegate methods work happily.

I've got a solution which will allow me to move the Alert into the parent view's methods, but I was hoping not to have to. Is this limitation real, or is it my imagination? IE am I doing something else wrong?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Ben Carroll
  • 138
  • 2
  • 11
  • the delegate can be any kind of object, generally an `id`, but they have to conform the `UIAlertViewDelegate` protocol. – holex Aug 31 '13 at 23:38
  • Thanks. I'm confused but appreciative! At least I know my theory is wrong :/ – Ben Carroll Sep 01 '13 at 05:33
  • Well, I'm not sure I like the solution--primarily because I'm not 100% certain why it works better--but I ended up putting the UIAlert call in the view object, but still making the subordinate (a sort of store kit addidtional abstraction layer) my delegate. So now I show my IAP titles and descriptions in the alert view like I want, and hitting "OK" sends me to the purchase. Thanks, as always, for indulging me! – Ben Carroll Sep 02 '13 at 21:25

0 Answers0