在UIAlertView中添加三个按钮

时间:2010-12-21 06:21:32

标签: iphone objective-c ios

我创建了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];

6 个答案:

答案 0 :(得分:35)

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

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

答案 1 :(得分:11)

你也可以让你的班级成为UIAlertViewDelegate的代表,并通过这样做来达到所有按钮的功能;

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

答案 2 :(得分:3)

尝试在otherButtonTitles中添加按钮

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

答案 3 :(得分:1)

在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)

答案 4 :(得分:1)

试试这个:

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

答案 5 :(得分:0)

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)