Pregunta

I am trying to set up an alert view so that when the "Ok" button is pressed, an action sheet comes up with two options. I believe i have it in the right format and there are no errors, but when I run it, nothing happens. please help and thank you in advanced.

-(IBAction)sendSG:(id)sender{
    UIAlertView *message = [[UIAlertView alloc]
                            initWithTitle:@"Send Study Guides!"
                            message:@"Please send your study guides to help create a bigger and more efficent network of study guides. You can send them by email, or you can take a picture of your study guide and send it to us."
                            delegate:self //Changed Here
                            cancelButtonTitle:@"Cancel"
                            otherButtonTitles:@"Ok", nil];

    [message show];

}





-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {
        UIActionSheet *sendOptions = [[UIActionSheet alloc]

                                      initWithTitle:@"Add study guide"
                                      delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      destructiveButtonTitle:@"Destructive Button"
                                      otherButtonTitles:@"Email", @"Take a picture", nil];

        [sendOptions showInView:self.view];
        }   

    }



    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 0) {
        NSString *emailTitle = @"Study Guides";

        NSArray *toRecipents = [NSArray arrayWithObject:@"blank@gmail.com"];

        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

        [mc setSubject:emailTitle];

        [mc setToRecipients:toRecipents];

        [self presentViewController:mc animated:YES completion:NULL];

        }
    }
¿Fue útil?

Solución

  1. set Delegate of your alert view to self. In your code,

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex is not being called due to this

  2. Yes, and change your button index too… forgot to tell you that. it should be 1 for both alertView & actionSheet.

  3. Make your viewController ActionSheet Delegate. add "UIActionSheetDelegate" in your viewController.h

    @interface XYZViewController : UIViewController UIActionSheetDelegate (enclose in angular braces)

Everything Else will work fine.. Let me know if there is any issue

Otros consejos

You got the wrong button index. The button buttonIndex == 0 is the cancel button,buttonIndex == 1 is the ok button.

Try this in alert view's callback:

double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // show action sheet here.

});

If it works for you, modify the delayInSeconds as you wish.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top