2

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

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

4

1 回答 1

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)

}
于 2015-05-18T12:44:11.053 回答