如何处理UIAlertController的操作?

时间:2016-03-25 19:03:42

标签: ios objective-c uialertcontroller

如何使用objective-c将UIAlertController警报操作按钮与操作处理程序链接?我正在使用Xcode 7.1。

这是我的代码:

- (IBAction)selectbtn:(UIButton *)sender {

    UIAlertController *alert=[ UIAlertController alertControllerWithTitle:@"NEW" message:@"button pressed" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraaction=[UIAlertAction actionWithTitle:@"From camera" style:UIAlertActionStyleDefault handler:nil ];
    [alert addAction:cameraaction];
    UIAlertAction *libraryaction=[UIAlertAction actionWithTitle:@"From photo library" style:UIAlertActionStyleDefault handler:nil ];
    [alert addAction:libraryaction];
    UIAlertAction *cancelaction=[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:cancelaction];
    [self presentViewController:alert animated:YES
                     completion:nil];
    }

3 个答案:

答案 0 :(得分:32)

<强>目标C

UIAlertController的工作原理如下:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"text mssg" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
    // Ok action example
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
    // Other action
}];
[alert addAction:okAction];
[alert addAction:otherAction];
[self presentViewController:alert animated:YES completion:nil];

我认为你的意思是。

Swift 3.0 / 4.0

let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertControllerStyle.alert)

myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in
        print("Selected")
    })
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in
        print("Cancel")
    })

    self.present(myalert, animated: true)

答案 1 :(得分:4)

您可以在action方法的处理程序中添加所需的任何代码,示例代码可以是这样的:

data depress;
set "C:\Users\Air\Desktop\652\depress.sas7bdat";
retain num1 0;
retain num2 0;
retain num3 0;
retain num4 0;
if (SEX=2 and DRINK=1) then do; num1=sum(num1,1);
if (SEX=2 and DRINK=2) then do; num2=sum(num2,1);
if (SEX=1 and DRINK=1) then do; num3=sum(num3,1);
if (SEX=1 and DRINK=2) then do; num4=sum(num4,1);
num5=num1+num2;
num6=num3+num4;
num7=num1+num3;
num8=num2+num4;
"num1 is ", num1;
"num2 is ", num2;
"num3 is ", num3;
"num4 is ", num4;

Ref Link

答案 2 :(得分:0)

具有多个textField的UIAlertView控制器。

 @IBAction func showAlert(_ sender: UIButton) {
        let alert = UIAlertController(title: "Registration", message: "Enter your name!", preferredStyle: .alert)
        alert.addTextField { (textField1: UITextField) in
            textField1.placeholder = "John"
        }
        alert.addTextField { (textField2: UITextField) in
            textField2.placeholder = "Doe"
        }
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction) in
            if let textField1 = alert.textFields?.first {
                if textField1.text == "" {
                    print("you have to enter something first")
                }else {
                    print("Hello \(textField1.text!)")
                }
            }
            if let textField2 = alert.textFields?.first {
                if textField2.text == "" {
                    print("you have to enter something first")
                }else {
                    print("Hello \(textField2.text!)")
                }
            }
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }