Laaaa
Laaaa

Reputation: 3

UIAlertView. Using otherbutton as a openURL.

I want it once the user taps the 'otherbutton' it goes to my link. But at the moment once the button is pressed it doesn't do anything.

Sorry im on my iPhone so I've used pastebin for the code

http://pastebin.com/fvgk87ih

Thanks alot

Upvotes: 0

Views: 654

Answers (3)

Evan Mulawski
Evan Mulawski

Reputation: 55354

You need to set the UIAlertView's delegate property to self instead of nil and implement the protocol in the header.

Header File:

@interface MyViewController : UIViewController <UIAlertViewDelegate>

In addition, check for the buttonIndex. Currently, (after performing the above), it will open the URL if any button is pressed.

- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
       //open url
    }
}

Upvotes: 1

Alex Coplan
Alex Coplan

Reputation: 13371

Conform your controller class to the UIAlertViewDelegate protocol.

@interface MyViewController : UIViewController <UIAlertViewDelegate>

Create the alert view with delegate set to self:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow" message:@"me on twitter" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release]; 

Respond to the delegate method

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
       // open url
    }
}

Upvotes: 1

Faizan S.
Faizan S.

Reputation: 8644

The delegate of your UIAlertView must be set to the class (self in most cases) that's using it. Make sure your class calling the UIAlertView does conform to the UIAlertViewDelegate delegate.

Upvotes: 1

Related Questions