0

I want the cancel button of my UIAlertView to launch the app store so that my app can be updated. I can get the app to launch the app store, but I want it to launch only when the cancel button of my UIAlertView is pressed. The way I have it now, I'm given this error when I press the cancel button:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType alertView:clickedButtonAtIndex:]: unrecognized selector sent to instance 0x1651bd90'

Here's the code where I initialize and display the UIAlertView:

NSString* updateString = @"Please update the app!  Thank you!";
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Please Update" message:updateString delegate:self cancelButtonTitle:@"Update Now" otherButtonTitles:nil];

[alert show];

Here's the function that's supposed to handle the cancel button being pressed:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/appname"]];
}

I've written in the containing object's header file that it follows the UIAlertViewDelegate protocol.

What am I doing wrong here?

TheSneak
  • 500
  • 6
  • 15
  • have you release the alert object because currently your code seem to be perfect. – Sunny Shah May 08 '14 at 04:28
  • 1
    Is the UIAlertView being called from the main thread? – Darius Houle May 08 '14 at 04:28
  • http://stackoverflow.com/questions/10913036/uialertview-delegate-method-crashing – Kumar KL May 08 '14 at 04:29
  • If you're not using ARC you should be. – CrimsonChris May 08 '14 at 04:30
  • [This approach](http://nscookbook.com/2013/04/ios-programming-recipe-22-simplify-uialertview-with-blocks/) is by far the best way to use UIAlertViews. – CrimsonChris May 08 '14 at 04:31
  • I am using ARC so not certain whether it's getting released. The alert is being set up and shown from within the connectionDidFinishLoading method of a NSURLConnectionDelegate. – TheSneak May 08 '14 at 04:33
  • if you using ARC and its still releasing then you should declare in .h file. o – Sunny Shah May 08 '14 at 04:37
  • What class is the alert's delegate? Are you sure that the delegate object isn't being deallocated while the alert view is being displayed? – rmaddy May 08 '14 at 04:39
  • 1
    i think you should enable Zombie objects. – Sunny Shah May 08 '14 at 04:42
  • 1
    Your creation of alert view is correct so problem is else where :) – iPatel May 08 '14 at 04:43
  • Ok cool, so I had created an object to carry out the NSURLConnectionDelegate tasks, and one of the tasks was to get info from a server and then tell the user to update the app if certain info came back from the server. That NSURLConnectionDelegate was also my UIAlertViewDelegate. Looks like that NSURLConnectionDelegate object was getting deallocated before the button was pressed and that was causing the problem. I've now instead made my main ViewController the UIAlertViewDelegate and it has fixed the problem. Thanks for the help folks! – TheSneak May 08 '14 at 04:54
  • have you added in .h file? – Ashutosh May 08 '14 at 06:10

1 Answers1

-1

You might be released the object that was calling this piece of code. If you are using ARC please make it a property.

Agam
  • 44
  • 4
  • You're a little late to the party. Read the last comment by the OP. He already solved the issue. – rmaddy May 08 '14 at 05:12