2016-10-15 25 views
16

Mam następujące metody alertu.UIAlertController nie działa z 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) 
} 

dostaję błąd mówiąc, że nie jest to dodatkowy argument animated w metodzie presentViewController, ale kiedy go wyjąć, jeszcze nie odwołuje się błąd, a potem mi powiedziano, że jest completion dodatkowy argument.

Odpowiedz

26

presentViewController został zmieniony w Swift 3 w ten sposób.

present(alert, animated: true) 

Aby uzyskać więcej informacji, sprawdź numer Apple Documentation.

Od Swift 3 completion jest opcjonalne, więc jeśli nie chce obsługiwać bloku Zakończenie ma potrzeby pisania nil na to i jeśli chcesz obsługiwać bloku zakończenia następnie pisać w ten sposób.

self.present(alert, animated: true) { 

} 

Uwaga: Twoja metoda notifyUser jest zadeklarowana z static więc nie można użyć self z nim tak, że również usunąć, aby usunąć kolejny błąd, który otrzymasz po skorygowaniu tego.

+3

Dzięki man. Ta cała szybka 3 ma mnie na boki! Doceniam to! – BlackHatSamurai

+0

@ BlackCatSamurai Welcome mate :) –

+0

Twoje rozwiązanie jest standardowe, ale twoja odpowiedź nie jest zgodna z pytaniem. Próbuje przedstawić kontroler widoku przy użyciu metody statycznej. dlatego błąd w jego wykonaniu jest dodatkowym argumentem. –

26
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) 
+0

OP działa z Swift 3 Więc to nie będzie pomocne. –

+0

Po prostu zmienię go na szybki 3.0 daj mi 5 minut @NiravD –

+0

@NiravD zrobił to –

11

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

Swift 3 Spróbuj klienta Action Anuluj

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

Próbujesz samodzielnego stosowania w metodzie statycznej podczas samodzielnego powinien być obecny instancja tej klasy, ale statyczna nie może używać self.

bez działania Handler dla przycisku Alert

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

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

z działaniem Handler Alert Buttona

W 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 powyższej metody przykład takiego.

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

Tutaj yourViewController jest regulator, na którym zaprezentujesz ten alert

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

Wywołaj powyższą metodę statyczną jak ta.

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

Tutaj yourViewController jest regulator, na którym zaprezentujesz ten alert

+0

Jeśli spojrzysz na jego kod, będziesz wiedział, że chce pokazać alert w statycznej metodzie, ale jego tytuł pokazuje, że on także chce rozwiązania w szybkim 3. Więc kończę oba. –

+0

Proszę bardzo. Zaznaczam twoją odpowiedź w górę. –

+0

To nie chodzi o głosowanie w dół lub o głosowanie w górę Pytanie właśnie zasugerowałem rozwiązanie problemu związanego z błędem zamieszczonym w pytaniu, a nie nic innego. –