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

Xcode 如何向警报视图按钮添加操作?,xcode,ios7,xcode5,uialertview,uiactionsheet,Xcode,Ios7,Xcode5,Uialertview,Uiactionsheet,我正在尝试设置一个警报视图,这样当按下“Ok”按钮时,一个动作表会显示两个选项。我相信我有正确的格式,没有错误,但当我运行它时,什么也没有发生。请提前帮助并感谢您 -(IBAction)sendSG:(id)sender{ UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Send Study Guides!"

我正在尝试设置一个警报视图,这样当按下“Ok”按钮时,一个动作表会显示两个选项。我相信我有正确的格式,没有错误,但当我运行它时,什么也没有发生。请提前帮助并感谢您

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

        }
    }

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

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

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.

});
如果它对您有效,请根据需要修改delayUnseconds

  • 将警报视图的委托设置为self。 在代码中

    -(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引 因此未被调用

  • 是的,还更改了按钮索引…忘了告诉你了。alertView和actionSheet都应为1

  • 使您的viewController操作表成为委托。在viewController.h中添加“UIActionSheetDelegate”

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


  • 其他一切都会好的。。如果有任何问题,请告诉我

    是的,很抱歉回复太晚,但这很有效!非常感谢你的帮助!