如何向警报视图按钮添加操作?

时间:2013-12-27 02:33:18

标签: xcode ios7 xcode5 uialertview uiactionsheet

我正在尝试设置警报视图,以便在按下“确定”按钮时,操作表会出现两个选项。我相信我有正确的格式并且没有错误,但是当我运行它时,没有任何反应。请提前帮助并感谢您。

-(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];

        }
    }

3 个答案:

答案 0 :(得分:1)

  1. 将警报视图的委托设置为self。 在您的代码中,

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 由于这个

  2. 而未被调用
  3. 是的,也改变你的按钮索引......忘了告诉你。对于alertView和amp;它应该是1 actionSheet。

  4. 制作你的viewController ActionSheet委托。在viewController.h中添加“UIActionSheetDelegate”

    @interface XYZViewController:UIViewController UIActionSheetDelegate(用角括号括起来)

  5. 其他一切都会正常工作..如果有任何问题,请告诉我

答案 1 :(得分:0)

你的按钮索引错误了。 按钮buttonIndex == 0是取消按钮,buttonIndex == 1是确定按钮。

答案 2 :(得分:0)

在警报视图的回调中尝试此操作:

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.

});

如果它适合您,请根据需要修改delayInSeconds。