ios 在 UIAlertView 中添加三个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4496750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:21:33  来源:igfitidea点击:

Add three Buttons in an UIAlertView

iphoneobjective-cios

提问by Velmurugan

I created uialertview, and add two buttons, Now I need to add one more button in alert view. How to edit my code to add one more button?

我创建了 uialertview,并添加了两个按钮,现在我需要在警报视图中再添加一个按钮。如何编辑我的代码以再添加一个按钮?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Refresh" message:@"Are you want to Refresh Data" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

回答by EmptyStack

If you are really struggling to find the solution, the following code may help you.

如果您真的很难找到解决方案,以下代码可能会对您有所帮助。

UIAlertView *alert = [[UIAlertView alloc] 
                        initWithTitle:@"Refresh" 
                              message:@"Are you want to Refresh Data" 
                             delegate:self 
                    cancelButtonTitle:@"Cancel" 
                    otherButtonTitles:@"OK", @"Done", nil];
[alert show];
[alert release];

回答by Engnyl

You can also make your class the delegate of UIAlertViewDelegate and reach all of your buttons" functionalities by doing so;

你还可以让你的类成为 UIAlertViewDelegate 的委托,并通过这样做来达到你所有的按钮”功能;

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        //
    }
    else if (buttonIndex == 1) {
        //
    }
    else if (buttonIndex == 2) {
        //
    }
    else if (buttonIndex == 3) {
        //
    }
}

回答by iPrabu

Try to add your buttons in otherButtonTitles

尝试在 otherButtonTitles 中添加您的按钮

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Refresh" message:@"Are you want to Refresh Data" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1",@"Button2"@"Button2",nil];

回答by Josh

In iOS11 / Swift 4, it is as simple as this:

在 iOS11 / Swift 4 中,就这么简单:

let alert = UIAlertController(title: "Saving Demo.", message: "Do you wish to save this Demo Settings?", preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
                (_)in
                //SAVE DEMO DATA HERE       
            })
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: {
                (_)in
                //do something
            })
            let thirdAction = UIAlertAction(title: "3rd Btn", style: UIAlertActionStyle.default, handler: {
                (_)in
                //do another thing
            })
            alert.addAction(OKAction)
            alert.addAction(cancelAction)
            alert.addAction(thirdAction)
            self.present(alert, animated: true, completion: nil)

回答by Nick

Try This:

尝试这个:

UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:@"Refresh"
                              message:@"Are you want to Refresh Data" 
                           preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             // code here...
                         }];

UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"Ok"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             // code here...
                         }];

UIAlertAction* done = [UIAlertAction
                         actionWithTitle:@"Done"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             // code here...
                         }];

[alert addAction:ok] ;
[alert addAction:done];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];

回答by Team chang

let alert = UIAlertController(title: "title",
                                      message: "message",
                                      preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Restore Purchases", style: .default, handler: {(action: UIAlertAction!) in

            //function to execute when button is clicked

        }))

        alert.addAction(UIAlertAction(title: "Subscribe", style: .default, handler: {(action: UIAlertAction!) in

            //function to execute when button is clicked

        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: {(action: UIAlertAction!) in

            //function to execute when button is clicked
        }))

        self.present(alert, animated: true, completion: nil)