UIAlertController 不适用于 Swift 3.0

UIAlertController Not Working with Swift 3.0

我有以下提醒方法。

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

我得到一个错误,说presentViewController方法中有一个额外的参数animated,但是当我把它拿出来时,它仍然没有消除错误,然后我' m 告诉 completion 是一个额外的参数。

presentViewController在Swift3中改成这样。

present(alert, animated: true)

查看 Apple Documentation 了解更多详情。

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

self.present(alert, animated: true) { 

}

注意: 您的 notifyUser 方法是用 static 声明的,因此您不能将 self 与它一起使用,因此删除它也可以删除您遇到的下一个错误纠正这个后得到。

 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)

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

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)

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

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

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

在Swift3.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)
    }

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

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

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

[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 yourViewController is the controller on which you will present this Alert

在Swift3.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)
    }
}

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

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 yourViewController is the controller on which you will present this Alert

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)