hey_suburbia
hey_suburbia

Reputation: 386

Second AlertView openURL doesn't work

I have two alert buttons, I can't get the second button to go to a different URL, it just goes to the same URL as the first button. The second alert pops up no problem, the "Visit" button on the second alert goes to the same as the first.

-(IBAction)creditBtn: (id) sender{  
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credits"
                                               message:@"Message
                                               delegate:self 
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"Visit", nil];    

     [alert show];                            
     [alert release];
}                         

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
      if(buttonIndex==1) {
         [[UIApplication sharedApplication] openURL:
                         [NSURL URLWithString:@"http://website1.com"]];
       }
}

-(IBAction)sendBtn: (id) sender2{   
    UIAlertView *alert2 = [[UIAlertView alloc]
                          initWithTitle:@"Send Me Your Feedback" 
                          message:@"Message"
                          delegate:self 
                          cancelButtonTitle:@"Cancel" 
                          otherButtonTitles:@"Visit", nil]; 
    [alert2 show];
    [alert2 release];
}

- (void)alertView2:(UIAlertView *)alertView2 clickedButtonAtIndex:(NSInteger)buttonIndex{
    // resign the shake FirstResponder, no keyboard if not
    //[self resignFirstResponder];
    // making the otherButtonTitles button in the alert open a URL
    if(buttonIndex==0){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website2.com"]];
    }
}

Upvotes: 1

Views: 708

Answers (1)

Andrew
Andrew

Reputation: 3892

Your problem is with the alertView2 delegate method. A delegate is a method that gets automatically called when something happens. In this case, the delagate methood:- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex gets automatically called when a UIAlertView is closed. So your problem is your alert2 is also calling the same delegate method as your first alert.

But there is a really easy fix. To fix it, we add a tag to each alertView. Then in the delegate method, we check the tag to see which alert it was. Here is how to do that:

//Set up your first alertView like normal:
-(IBAction)creditBtn: (id) sender{  
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credits" 
                                                message:@"Message"
                                                delegate:self
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:@"Visit", nil]; 
      alert.tag = 0;    //Here is that tag
      [alert show];                             
      [alert release]; //Although this is unnecessary if you are using iOS 5 & ARC
}

The only thing I changed is I tagged the first alert as alert 0. What this means is as long as each alert has a different tag, we can tell the difference.

Then set up your second alertView just like you were doing:

-(IBAction)sendBtn:(id)sender{   
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Send Me Your Feedback" 
                          message:@"Message"
                          delegate:self 
                          cancelButtonTitle:@"Cancel" 
                          otherButtonTitles:@"Visit", nil];
    alert.tag = 1;     //Notice I used a different tag number
    [alert show];
    [alert release];
}

Notice I named both alertViews alert. I didn't have to name them alert & alert2 because theres this thing called variable scope. Variable scope means variables live for a certain amount of time, and then die. So in your case, becuase you created the variable UIAlertView *alert inside a method, at the end of that method, that variable dies. For more infomation on that, check out this: Article on Variable Scope

Then finally, the delegate method that responds to the alertView being closed:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if(alert.tag == 0 && buttonIndex == 1)     //Show credits
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website1.com"]];

        else if(alert.tag == 1 && buttonIndex == 1)     //Send feedback
               [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website2.com"]];
 }

Thats all there is to it. Let me know in a comment if you need more help.

Upvotes: 1

Related Questions