Iphone 在UIAlertView中添加三个按钮

Iphone 在UIAlertView中添加三个按钮,iphone,objective-c,ios,Iphone,Objective C,Ios,我创建了uialertview,并添加了两个按钮,现在我需要在AlertView中再添加一个按钮。如何编辑我的代码以添加更多按钮 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Refresh" message:@"Are you want to Refresh Data" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [aler

我创建了uialertview,并添加了两个按钮,现在我需要在AlertView中再添加一个按钮。如何编辑我的代码以添加更多按钮

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

尝试将您的按钮添加到其他按钮中

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

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

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

您还可以使您的类成为UIAlertViewDelegate的委托,并通过这样做实现所有按钮的功能

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

在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)
试试这个:

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

您好,BuildSuccessed,谢谢您的回复。它确实工作得很好。如果您想触摸,请购买iphone或可能是iOS 9.0中不推荐的ipod touchUIAlertView。请使用UIAlertController。不鼓励只使用代码的答案,因为它们不会为未来的读者提供太多信息。请对您所写的内容提供一些解释
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)