ios UIAlertController 不适用于 Swift 3.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40055488/
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-31 10:35:30  来源:igfitidea点击:

UIAlertController Not Working with Swift 3.0

iosswiftswift3uialertcontroller

提问by BlackHatSamurai

I have the following alert method.

我有以下警报方法。

static func notifyUser(_ title: String, message: String) -> Void
{
    let alert = UIAlertController(title: title,
                                  message: message,
                                  preferredStyle: UIAlertControllerStyle.alert)

    let cancelAction = UIAlertAction(title: "OK",
                                     style: .cancel, handler: nil)

    alert.addAction(cancelAction)
    self.presentViewController(alert, animated: true, completion: nil)
}

I get an error saying that there is an extra argument animatedin the presentViewControllermethod, but when I take it out, it still doesn't dismiss the error, and then I'm told that completionis an extra argument.

我得到一个错误,说是有一个额外的参数animatedpresentViewController方法,但是我把它拿出来的时候,它仍然没有清除错误,然后有人告诉我completion是一个额外的参数。

回答by Nirav D

presentViewControlleris changed in Swift 3 like this.

presentViewController在 Swift 3 中是这样改变的。

present(alert, animated: true)

Check Apple Documentationfor more details.

有关更多详细信息,请查看Apple 文档

From Swift 3 completionis optional so if you doesn't want handle the completion block no need to write nilfor that and if you want to handle completion block then write like this.

从 Swift 3 开始completion是可选的,因此如果您不想处理完成块,则无需为此编写nil,如果您想处理完成块,则可以这样编写。

self.present(alert, animated: true) { 

}

Note:Your notifyUser method is declared with staticso you cannot use selfwith it so remove that also to remove the next error that you get after correcting this one.

注意:您的 notifyUser 方法是用 with 声明的,static因此您不能将其self与它一起使用,因此删除该方法也是为了删除您在更正此错误后遇到的下一个错误。

回答by Himanshu Moradiya

 let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .alert)
 let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
            //Just dismiss the action sheet
        }
 actionSheetController.addAction(cancelAction)
 self.present(actionSheetController, animated: true, completion: nil)

回答by Giang

Swift 3

斯威夫特 3

let alertView = UIAlertController(title: "", message: "your message", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { (alert) in

})
alertView.addAction(action)
self.present(alertView, animated: true, completion: nil)

回答by Syed Qamar Abbas

You are trying to use self in a static method while self should be the current instance of that class but static cannot use self.

您正在尝试在静态方法中使用 self,而 self 应该是该类的当前实例,但静态不能使用 self。

Without Action Handler For Alert Button

没有警报按钮的动作处理程序

In Obj-C

在 Obj-C 中

+(void) notifyUser:(NSString *)title withMessage:(NSString *)message onViewController:(UIViewController *)vc {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    //vc will be the view controller on which you will present your alert as you cannot use self because this method is static.
    [vc presentViewController:alert animated:true completion:nil];
}

In Swift 3.0

在 Swift 3.0 中

static func notifyUser(_ title: String, message: String, vc: UIViewController) -> Void
    {
        let alert = UIAlertController(title: title,
                                      message: message,
                                      preferredStyle: UIAlertControllerStyle.alert)

        let cancelAction = UIAlertAction(title: "OK",
                                         style: .cancel, handler: nil)

        alert.addAction(cancelAction)
        //vc will be the view controller on which you will present your alert as you cannot use self because this method is static.
        vc.present(alert, animated: true, completion: nil)
    }

With Action Handler For Alert Button

带有警报按钮的动作处理程序

In Obj-C

在 Obj-C 中

+(void) notifyUser:(NSString *)title withMessage:(NSString *)message withButtonTitles:(NSArray<NSString *> *)buttonTitles andButtonStyles:(NSArray *)styles onViewController:(UIViewController *)vc onCompletion:(void (^)(NSInteger))completion  {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

    for (NSString *title in buttonTitles) {
        UIAlertActionStyle style = [[styles objectAtIndex:[buttonTitles indexOfObject:title]] intValue];

        UIAlertAction *actionObj = [UIAlertAction actionWithTitle:title style:style handler:^(UIAlertAction *action){
            NSInteger index = [buttonTitles indexOfObject:action.title];
            completion(index);
        }];
        [alert addAction:actionObj];
    }
    //vc will be the view controller on which you will present your alert as you cannot use self because this method is static.
    [vc presentViewController:alert animated:true completion:nil];
}

Invoke above instance method like this.

像这样调用上面的实例方法。

[ClassName notifyUser:@"Title For Alert" withMessage:@"Message for Alert" withButtonTitles:@[@"Camera",@"Library",@"Dismiss"] andButtonStyles:@[@(UIAlertActionStyleDefault),@(UIAlertActionStyleDefault),@(UIAlertActionStyleCancel)] onViewController:yourViewController onCompletion:^(NSInteger indexOfTappedButton){
                    //Note the index of the button will have the same order as you have provide the titles array in this method
    }];

Here yourViewControlleris the controller on which you will present this Alert

yourViewController是您将在其上显示此警报的控制器

In Swift 3.0

在 Swift 3.0 中

extension UIAlertController {
    static func notifyUser(_ title: String, message: String, alertButtonTitles: [String], alertButtonStyles: [UIAlertActionStyle], vc: UIViewController, completion: @escaping (Int)->Void) -> Void
    {
        let alert = UIAlertController(title: title,
                                      message: message,
                                      preferredStyle: UIAlertControllerStyle.alert)

        for title in alertButtonTitles {
            let actionObj = UIAlertAction(title: title,
                                          style: alertButtonStyles[alertButtonTitles.index(of: title)!], handler: { action in
                                            completion(alertButtonTitles.index(of: action.title!)!)
            })

            alert.addAction(actionObj)
        }


        //vc will be the view controller on which you will present your alert as you cannot use self because this method is static.
        vc.present(alert, animated: true, completion: nil)
    }
}

Invoke above static method like this.

像这样调用上面的静态方法。

UIAlertController.notifyUser("Title for Alert", message: "Message show in Alert", alertButtonTitles: ["Camera", "Library","Dismiss"], alertButtonStyles: [.default,.default,.cancel], vc: yourViewController, completion: { indexOfTappedButton in
            //Note the index of the button will have the same order as you have provide the titles array in this method
        })

Here yourViewControlleris the controller on which you will present this Alert

yourViewController是您将在其上显示此警报的控制器

回答by amiron

Swift 3Try CustomAction Cancel

Swift 3尝试自定义操作取消

  let uiAlertController = UIAlertController(// create new instance alert  controller
          title: "You TITLE text",
          message: "You Message text", 
          preferredStyle:.alert)

    uiAlertController.addAction(// add Custom action on Event is Cancel
    UIAlertAction.init(title: "Cancel", style: .default, handler: { (UIAlertAction) in
       //TO DO code
       uiAlertController.dismiss(animated: true, completion: nil)//dismiss show You alert, on click is Cancel
    }))
    //show You alert
    self.present(uiAlertController, animated: true, completion: nil)

回答by Anju Sathish

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
if action {
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: {
        (action : UIAlertAction!) in self.navigationController?.popViewController(animated: true)
    }))
} else {
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
}
self.present(alert, animated: true, completion: nil)