Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
I have a button that I have placed in Interface Builder. The code that is uses is:

Header File
Code:
- (IBAction)openBrowser;

Main File
Code:
-(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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Have you set the class of the FileOwner correctly in IB? Also do IBActions not usually take a single argument of type id?
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
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:
Code:
-(IBAction)aboutPopup 
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

Any ideas?
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
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

macrumors 68020
Aug 23, 2008
2,005
3
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:
Code:
-(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

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
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:

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

@end

Then

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

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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 ...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.