-3

EXTRA INFO: I must have asked wrong, I'd like the user to click the button and be redirected to Facebook!

I need to add a visit facebook button called "facebook" as swell in the code below! Right now I just have an ok button.

Also if possible could you help me understand, how I will be able to retrieve user information - e.g I'll ask them to add there email in the textfield, when they press ok then where will I store the email and obtain it, how does that work, and what do I need to read to find out?

Ricky
  • 41
  • 5

3 Answers3

3

Try to add your buttons in otherButtonTitles

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Refresh" 
                                                message:@"Are you want to Refresh Data" 
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel" 
                                      otherButtonTitles:@"Button1",@"Button2",@"Button3",
                      nil];
Ilario
  • 5,979
  • 2
  • 32
  • 46
Erhan
  • 908
  • 8
  • 19
  • Yes I want the button to take them to facebook page! – Ricky Apr 18 '14 at 12:14
  • You can use the delegate of UIAlertView - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex. In this delegate check the UIButton index and make an URLConnection. – user2071152 Apr 18 '14 at 12:15
  • @Ricky do you wish to people create code for you..? that logic in answer is correct. now find how to implement facebook. – Nitin Gohel Apr 18 '14 at 12:16
  • @Ricky, that kind of service is not provided here. We are here to HELP you, not give you the answers. Giving code does not help you in programming. Having an issue in a program, and then finding the solution is what will help you – user2277872 Apr 18 '14 at 13:02
1

As the other poster said, you can add additional buttons by providing button titles in the otherButtonTitles parameter to the initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: method.

You can set up your alert to have an input field by setting the alertViewStyle property of the button returned by the above method to UIAlertViewStylePlainTextInput

You need to pass in self (your view controller) as the delegate parameter.

Then you need to implement the method alertView:didDismissWithButtonIndex:

In that method, you will get the button index the user selected. You can use the textFieldAtIndex: method to get a pointer to the text field, and get the user-entered text.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I must have asked wrong, I'd like the user to click the button and be redirected to Facebook! – Ricky Apr 18 '14 at 12:23
  • That's not how alerts work. You need to implement the `alertView:didDismissWithButtonIndex:` method. In that method, you write code that detects that the user clicked on the Facebook button, and if so, then you'd redirect them to Facebook. – Duncan C Apr 18 '14 at 12:34
1

As per Duncan's answer you can use delegate and get which button is clicked.so on specific button's tap you can redirect to facebook page using below code.

UIAlertView *info = [[UIAlertView alloc]initWithTitle:@"Yup" message:@"You've won! Like Us on Facebook too" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Facebook", nil];
info.tag = 10;
[info show];

So when the user presses the Facebook button the delegate method alertView:clickedButtonAtIndex will be called so at that time check alert.tag and then check if facebook button is tapped then show another alert.

Your Delegate Method should be

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10)
    {
        switch (buttonIndex) {
        case 0:
            NSLog(@"Cancel");
            break;
        case 1:
            NSLog(@"Facebook");
            [self showAlertForRedirect];
            break;
        default:
            break;
        }
    }
    else if (alertView.tag == 20)
    {
        switch (buttonIndex) {
        case 0:
            NSLog(@"Cancel");
            break;
        case 1:
            NSLog(@"OK");
            [self RedirectNow];
            break;
        default:
            break;
        }
    }
}
-(void)showAlertForRedirect
{
    UIAlertView *info2 = [[UIAlertView alloc]initWithTitle:@"Note" message:@"Would you like to redirect on facebook?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    info2.tag = 20;
    [info2 show];
}
-(void)RedirectNow
{
    NSURL *fanPageURL = [NSURL URLWithString:@"fb://profile/yourid"];
    if (![[UIApplication sharedApplication] openURL: fanPageURL])
    {
        NSURL *webURL = [NSURL URLWithString:@"https://www.facebook.com/yourpagename"];
        [[UIApplication sharedApplication] openURL: webURL];
    }
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
i-Maddy
  • 84
  • 4
  • hi man, this is just taking me directly to facebook without asking me if I want to go! – Ricky Apr 18 '14 at 12:55
  • its still redirecting me automatically – Ricky Apr 18 '14 at 13:15
  • @Ricky then what you want? how should app ask you that you want to redirect to facebook or not – i-Maddy Apr 18 '14 at 13:21
  • Yes I want them to click the button to go to facebook, but its not even showing the alert view if i use the format above, instead it automatically pushes use to facebook withou asking! – Ricky Apr 18 '14 at 13:33
  • im getting an error for this: -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex – Ricky Apr 18 '14 at 13:58