Reputation: 31
I'm getting an error on my alert.addAction(). Seems like my project doesn't know where to find it, am I right?
I made an alert without buttons, now I'm trying to add buttons to it.
So this is my alert window and the code to add the buttons:
func naamInModelChangedHandler ( notification:NSNotification ) {
println("De naam in de model is veranderd naar \(model.naam!)")
NSNotificationCenter.defaultCenter().removeObserver(
self,
name: "NAAM_CHANGED",
object: model)
let alert = UIAlertController(title: "Ola", message: "De naam is gewijzigd", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
alert.addAction(okAction)
alert.addAction(cancelAction)
}
I've made a UIAlertAction with the name AlertController with some code to make the buttons.
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
println("Ok geklikt")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
println("Cancel geklikt")
}
But it seems it doesn't know where to look for I think. What am I doing wrong here?
Upvotes: 2
Views: 2461
Reputation: 3000
Do like this
func naamInModelChangedHandler ( notification:NSNotification ) {
println("De naam in de model is veranderd naar \(model.naam!)")
NSNotificationCenter.defaultCenter().removeObserver(
self,
name: "NAAM_CHANGED",
object: model)
let alert = UIAlertController(title: "Ola", message: "De naam is gewijzigd", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
println("Ok geklikt")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
println("Cancel geklikt")
}
alert.addAction(okAction)
alert.addAction(cancelAction)
}
Upvotes: 2