PDA

View Full Version : UIAlertView Issue (Solved!)




matthew858
Apr 18, 2009, 01:33 AM
I have a button that I have placed in Interface Builder. The code that is uses is:

Header File

- (IBAction)openBrowser;


Main File

-(IBAction)openBrowser {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://macrumors.com"]];
}


The button is connected to File's Owner, which is linked to the main file that the above code comes from.

It seems alright, but when I run it in on a device, I get the following:


<Error>: *** -[UIViewController openBrowser]: unrecognized selector sent to instance 0x119f40


What have I done wrong?



robbieduncan
Apr 18, 2009, 03:18 AM
Have you set the class of the FileOwner correctly in IB? Also do IBActions not usually take a single argument of type id?

matthew858
Apr 18, 2009, 03:43 AM
Have you set the class of the FileOwner correctly in IB? Also do IBActions not usually take a single argument of type id?

I have set the class to "SettingsViewController" which is where my data is coming from. I thought you didn't need so set the argument of type id when using openURL?

Edit:

I fixed this issue, but I have another one. I can now get the alert to show, but when I click "OK" the app freezes and I need to quit it. The code I'm using is:

-(IBAction)aboutPopup
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}


Any ideas?

BlackWolf
Apr 18, 2009, 06:05 AM
pretty simple: you show the alert, then you release it. so when your button is pressed, the alert-object is already destroyed.

you should set a delegate that conforms to the "UIAlertViewDelegate" Protocol. then you can use alertView:clickedButtonAtIndex: to get a clicked button (in this case you only have one so that's pretty simple :D) and if you don't need the alertView after that you should be able to release it inside that method (I didn't test it, it could be possible that the alert view is still need because of the animation that dismisses it, but I guess releasing it in alertView:clickedButtonAtIndex: should work. try it and let me know! I'm not on my mac right now so I can't test it unfortunatly.)

If that doesn't work you can release the view in alertView:didDismissWithButtonIndex: btw

PS: I made that very same mistake when I first used an alertview :D

matthew858
Apr 18, 2009, 06:33 AM
I only have one issue with that, and that's I already have another UIAlertView declared in the same file. How do I tell the two apart?

BlackWolf
Apr 18, 2009, 08:05 AM
I only have one issue with that, and that's I already have another UIAlertView declared in the same file. How do I tell the two apart?

several ways to do it.
for example, you could store your alertviews in instance variables and then just see if the alertView variable in alertView:clickedButtonAtIndex: is the same as your instance variable.

or you could, for example, examine the alertview's message. if the message is <Alert message> then you have one view, and when the message is something else it's the other view.
or you can inspect the title if your view's have one, or the number of buttons ... just take something that's different in the two views and check for it.

matthew858
Apr 18, 2009, 05:04 PM
several ways to do it.
for example, you could store your alertviews in instance variables and then just see if the alertView variable in alertView:clickedButtonAtIndex: is the same as your instance variable.


What is the best way of doing this? Setting the value of a variable when executing the alert, then checking for the value of the variable when running clickButtonAtIndex?

CocoaPuffs
Apr 18, 2009, 05:44 PM
I have set the class to "SettingsViewController" which is where my data is coming from. I thought you didn't need so set the argument of type id when using openURL?

Edit:

I fixed this issue, but I have another one. I can now get the alert to show, but when I click "OK" the app freezes and I need to quit it. The code I'm using is:

-(IBAction)aboutPopup
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}


Any ideas?
delegate should just be nil. As for releasing it, that's the right way to do it, since the alert is only relevant within the method that is being called, retaining it is pointless.

Other than that, I don't see any other reason why your app is frozen, could be something else entirely. Is there another line of code under the alert that may prompt the app to crash?

Niiro13
Apr 18, 2009, 05:47 PM
pretty simple: you show the alert, then you release it. so wh

You actually should release the alert after you show it. I don't think that's the problem as I do it all the time without fail (note that the documentation also does that as well as the sample code). It's probably what you said afterwards...how they don't have the delegate.

What is the best way of doing this? Setting the value of a variable when executing the alert, then checking for the value of the variable when running clickButtonAtIndex?

You could put both alertviews in the header file:

@interface RootViewController : UIViewController {
UIAlertView *firstAlert;
UIAlertView *secondAlert;
}

@end

Then

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == firstAlert) {
}
else { // or else if (alertView == secondAlert)
}
}

matthew858
Apr 19, 2009, 01:00 AM
Thanks, this worked a treat!

BlackWolf
Apr 19, 2009, 04:35 AM
You actually should release the alert after you show it. I don't think that's the problem as I do it all the time without fail

but where does the alertview in alertView:clickedButtonAtIndex: come from if you already released the view?
maybe the alertview is retained when you show it, I don't know about that. I will check that if I have the time on my mac later ...