2015-05-18 37 views
0

我在alert.addAction()上收到錯誤消息。似乎我的項目不知道在哪裏可以找到它,對嗎?使用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") 
} 

但似乎它不知道在哪裏尋找我認爲。我在這裏做錯了什麼?

+0

什麼錯誤? – fluidsonic

+0

你在哪裏宣佈你的行爲和你的戒備?向我們展示更多請 –

+0

您是否在alert.addAction(okAction)之前定義了okAction和cancelAction alert.addAction(cancelAction)? – Amit89

回答

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) 

} 
+0

哦,所以我把它放在相同的功能,而不是在另一個文件。好像我誤解了那部分。非常感謝! – MrGreyGoose

+0

是的,需要在使用前申報。如果它有幫助,你可以投票,它會幫助其他人作爲正確的答案。 :) – Amit89

+0

因爲我的聲望還不夠高,所以我還不能滿意,但我把它標記爲正確。 – MrGreyGoose