2014-06-06 5 views
3

quand je runt ce code dans swift, je ne sais pas pourquoi l'application se termine en montrant un point de rupture dans la partie "alertView.show()", Quelqu'un s'il vous plaît aidez-moi.UIAlertView ne fonctionne pas dans Swift

var alertView = UIAlertView(
    title: "Hey", 
    message: "Hello", 
    delegate: self, 
    cancelButtonTitle: "Cancel" 
) 
alertView.show() 

Répondre

17

De classe Xcode 6.0 UIAlertView:

UIAlertView est dépréciée. Utilisez UIAlertController avec un style préféré de UIAlertControllerStyleAlert à la place.

Sur rapide (iOS 8 et OS X 10.10), vous pouvez le faire:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel)) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in 
       println("User click Ok button") 
      })) 
     self.presentViewController(alert, animated: true, completion: nil) 

func handleCancel(alertView: UIAlertAction!) 
     { 
      println("User click cancel button") 
     } 

Si vous souhaitez utiliser dans 'ActionSheet' au lieu 'Alert' il vous suffit de changer le UIAlertControllerStyle pour exemple:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet) 
+0

Pouvez-vous me dire comment ajouter un TextField? –

+1

@ AppleKid, ajouter alert.addTextFieldWithConfigurationHandler (configurationTextField), et la fonction: func configurationTextField (textField: UITextField) { println ("Configurat location TextField") si laisser tField = {textField self.textField = champ de texte! self.textField.text = "Bonjour tout le monde" } } Maintenant vous pouvez imprimer l'entrée de l'utilisateur sur les fermetures handleCancel ou handleOk: println (self.textField.text) –

+0

@AppleKid, Ok? –

-1

Utilisation suivante façon:

var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 
altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil)) 
self.presentViewController(altMessage, animated: true, completion: nil) 
+0

utilisez "func showInView (_ view: UIView!)" Pour ajouter textField ici est ref. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html#//apple_ref/occ/instm/UIActionSheet/showInView: –

+0

je ne l'ai pas compris .. pouvez-vous s'il vous plaît montre moi un petit code. –

+0

Cela ne fonctionnera pas sur iOS 7. – Sulthan

1

UIAlertView est dépréciée dans iOS 8, mais Swift soutient et vous pouvez iOS 7 pas utiliser UIAlertController sur iOS 7. Ajoutez la méthode suivante pour résoudre le problème:

func showAlert(title:NSString, message:NSString,owner:UIViewController) { 
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") { 
     var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     owner.presentViewController(alert, animated: true, completion: nil) 
    } 
    else { 
     let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK") 
     alertView.alertViewStyle = .Default 
     alertView.show() 
    } 
} 

et appeler la méthode partout à partir du code comme ceci:

showAlert(APP_NAME,message: "Add your alert message here" ,owner: self) 
0

la meilleure façon pour moi est ...

class ViewController: UIViewController, UIAlertViewDelegate { 
var allarme = UIAlertView(title: "Warning", message: "This is a best way to create a alarm message", delegate: self, cancelButtonTitle: "OK") 
     allarme.show() 

se souviennent d'importer de la classe UIAlertViewDelegate

Questions connexes