2016-04-04 8 views
31

Ich habe mehr sehen so Antworten, aber nichts helped.Here ist mein älterer Alarm und Aktion für das"UIAlertView" wurde in iOS 9.0 nicht mehr unterstützt. Verwenden Sie UIAlertController mit einem preferredStyle von UIAlertControllerStyleAlert statt

override func viewWillAppear(animated: Bool) { 
    if Reachability.isConnectedToNetwork() == true { 
     print("internet connection ok") 
    } else 
    { 
     print("internet not ok") 
     let alertView: UIAlertView = UIAlertView(title: "Alert ", message: "connect to internet", delegate: self, cancelButtonTitle: "settings", otherButtonTitles: "cancel") 
     alertView.show() 
     return  
    }  
} 

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) 
{ 
    if buttonIndex == 0 { 
     //This will open ios devices wifi settings 
     UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!) 
    } 
    else if buttonIndex == 1 
    { 
     //TODO for cancel 
     exit(0) 
    } 
} 

Indem ich erhalte Warnung:

"UIAlertView" wurde in iOS 9.0 nicht mehr unterstützt. Verwenden Sie UIAlertController mit einem preferredStyle von UIAlertControllerStyleAlert statt

Ich habe versucht:

let alert = UIAlertController(title: "Alert", message: "My Alert for test", preferredStyle: UIAlertControllerStyle.Alert) 
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) in 
     print("you have pressed the Cancel button") 
    })) 
self.presentViewController(alert, animated: true, completion: nil) 

Aber zwei Schaltfläche hinzuzufügen und den Indexpfad der Tastendruck-Methode verknüpfen meinen älteren Code hinzufügen, ich bin nicht in der Lage zu tun Das. Keine Aktion passiert von meinem UIALERT-Taste,

Bitte helfen Sie mir, Wie kann ich diese Warnungen entfernen und meine Uialert mit meiner Zwei-Tasten-Aktion recode.

Ich bin neu bei swift.Ihre Hilfe wird nützlich sein. Danke!

+1

Warum brauchen Sie Knopfindex in obigem Fall? Schreiben Sie den Code, der für jeden dieser Schaltflächen in seinem Handler-Block/Closure ausgeführt werden muss. –

+0

@ user5513630 Wenn wir UIAlertCOntroller anstelle von UIAlertView verwenden, können wir dann diese Funktion "func alertView (alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {}" verwenden, wenn nicht dann, was ist die Alternative? Bitte helfen Sie mir, ich bin neu – ArgaPK

+0

@MidhunMP, wenn wir UIAlertCOntroller anstelle von UIAlertView verwenden, dann können wir diese Funktion "func alertView (alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {}" wenn nicht dann, was ist die Alternative? Bitte helfen Sie mir, ich bin neu – ArgaPK

Antwort

66

Reference UIAlertController

Siehe diesen Code Destructive und OK-Tasten in UIAlertController:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert 
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) { 
    (result : UIAlertAction) -> Void in 
    print("Destructive") 
} 

// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default 
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { 
    (result : UIAlertAction) -> Void in 
    print("OK") 
} 

alertController.addAction(DestructiveAction) 
alertController.addAction(okAction) 
self.presentViewController(alertController, animated: true, completion: nil) 

Swift 3:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert 

let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) { 
         (result : UIAlertAction) -> Void in 
    print("Destructive") 
} 

        // Replace UIAlertActionStyle.Default by UIAlertActionStyle.default 

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { 
         (result : UIAlertAction) -> Void in 
    print("OK") 
} 

alertController.addAction(DestructiveAction) 
alertController.addAction(okAction) 
self.present(alertController, animated: true, completion: nil) 

Siehe Alarm mit Destructive und OK-Taste:

enter image description here

+0

was ist mit Aktionsmethode? Muss ich separate Methode dafür erwähnen? – user5513630

+0

Nein Nicht.Dies ist neu in AlertController. Und Button-Aktionsereignis behandeln in dieser Methode. –

+0

zum Beispiel, wenn ich die OK-Taste drücke, muss ich diese Zeile 'UIApplication.sharedApplication() verwenden. OpenURL (NSURL (string:" prefs: root ")!)' Dann kann ich diese Zeile in Ihrem Code – user5513630

0

Versuchen Sie diesen Code. Ex

let actionSheetController: UIAlertController = UIAlertController(title: "Are you sure?", message: "", preferredStyle: .Alert) 
let cancelAction: UIAlertAction = UIAlertAction(title: "NO", style: .Cancel) { action -> Void in 
       //Do your task 
    } 
    actionSheetController.addAction(cancelAction) 
    let nextAction: UIAlertAction = UIAlertAction(title: "YES", style: .Default) { action -> Void in 
      //Do your task    //NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!) 
     //     NSUserDefaults.standardUserDefaults().synchronize() 
} 
actionSheetController.addAction(nextAction) 
self.presentViewController(actionSheetController, animated: true, completion: nil) 
+0

Wenn wir UIAlertCOntroller anstelle von UIAlertView verwenden, dann können wir diese Funktion verwenden "func alertView (alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {}" wenn nicht dann was ist die Alternative? Bitte helfen Sie mir, ich bin neu – ArgaPK

+0

@ArgaPK können Sie versuchen, Handler alert.addAction (UIAlertAction (Titel: "Okay", Stil: UIAlertActionStyle.Default, Handler: {(Warnung: UIAlertAction!) in println ("Ihr Code")})) –

0

Dieser Code

let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert) 

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) 
{ 
    (action:UIAlertAction!) in 
    print("you have pressed the Cancel button"); 
} 
alertController.addAction(cancelAction) 

let OKAction = UIAlertAction(title: "OK", style: .Default) 
{ 
    (action:UIAlertAction!) in 
       print("you have pressed OK button"); 
} 
alertController.addAction(OKAction) 

self.presentViewController(alertController, animated: true, completion:nil) 
+0

tut, wird Code automatisch meine Aktion Methode ???? – user5513630

+0

weil ich den Button click = 0,1 gesetzt habe bedeutet das ich etwas action gesetzt habe. also nur gefragt – user5513630

+0

NEIN, Sie müssen Ihre Aktionsmethode aufrufen, wo ich drucke (EX. "Sie haben die Abbrechen-Taste gedrückt"), wenn die Taste gedrückt wurde. Sie müssen diesen Code ändern. –

0

ich habe diese Antwort von diesem Link denken helfen, werde ich es ist nützlich für Sie

How to add an action to a UIAlertView button using Swift iOS

var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) 

// Create the actions 
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
    UIAlertAction in 
    NSLog("OK Pressed") 
} 
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
    UIAlertAction in 
    NSLog("Cancel Pressed") 
} 

// Add the actions 
alertController.addAction(okAction) 
alertController.addAction(cancelAction) 

// Present the controller 
self.presentViewController(alertController, animated: true, completion: nil) 
1
UIAlertController *AC = UIAlertController.alertControllerWithTitle("Title",message:"Message",preferredStyle:UIAlertControllerStyleAlert) 

    UIAlertAction *ActionOne = [UIAlertAction actionWithTitle:"ActionOne" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionOne") 
} ] 

    UIAlertAction *ActionTwo = [UIAlertAction actionWithTitle:"ActionTwo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionTwo") 
} ] 
AC.addAction(ActionOne) 
AC.addAction(ActionTwo) 
self.presentViewController(AC,animated:true,completion:nil) 
15

In Swift 3, können Sie schreiben:

let alertController = UIAlertController(title: "Title", message: "This is my text", preferredStyle: UIAlertControllerStyle.alert) 

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) 
{ 
    (result : UIAlertAction) -> Void in 
     print("You pressed OK") 
} 
alertController.addAction(okAction) 
self.present(alertController, animated: true, completion: nil) 
7

Für eine grundlegende Warnmeldung ich eine Erweiterung auf UIViewController mag mit:

extension UIViewController { 
func alertMessageOk(title: String, message: String) { 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
    let action = UIAlertAction(title: "Ok", style: .default, handler: nil) 
    alert.addAction(action) 
    present(alert, animated: true, completion: nil) 
    } 
} 

Verbrauch: self.alertMessageOk(title: "Test Title", message: "Test message")

2

Swift 3

// Create message 
    let alertController = UIAlertController(title: "Title", 
              message: "Message", 
            preferredStyle: .actionSheet) 

    // Clear Action 
    let clearAction = UIAlertAction(title: "Clear", 
            style: .destructive, 
            handler: { (action:UIAlertAction!) in 
         print ("clear") 
    }) 
    alertController.addAction(clearAction) 

    // Cancel 
    let cancelAction = UIAlertAction(title: "Cancel", 
            style: .cancel, 
            handler: { (action:UIAlertAction!) in 
           print ("Cancel") 
    }) 
    alertController.addAction(cancelAction) 

    // Present Alert 
    self.present(alertController, 
       animated: true, 
       completion:nil)