使用alert.addAction使用未解析的标识符

时间:2015-05-18 12:30:12

标签: swift alert uialertaction

我在alert.addAction()上收到错误。好像我的项目不知道在哪里找到它,我是对的吗?

我没有按钮发出警报,现在我正在尝试添加按钮。

所以这是我的警告窗口和添加按钮的代码:

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)

}

我使用名称AlertController创建了一个带有一些代码的UIAlertAction来制作按钮。

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

但它似乎并不知道我认为在哪里寻找。我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

这样做

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)

}