簡體   English   中英

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 ,但是當我把它取出時,它仍然沒有消除錯誤,然后我被告知completion是一個額外的參數。

presentViewController在Swift 3中就像這樣改變了。

present(alert, animated: true)

有關詳細信息,請查看Apple文檔

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

斯威夫特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)

您試圖在靜態方法中使用self,而self應該是該類的當前實例,但static不能使用self。

沒有Action Handler for Alert Button

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

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

使用Action Handler for Alert Button

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

在這里, yourViewController是您將在其上顯示此警報的控制器

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

像這樣調用上面的靜態方法。

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

在這里, yourViewController是您將在其上顯示此警報的控制器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM