2015-04-12 34 views
-2

我從我的添加欄按鈕項下面的代碼中調用了我提供的警報視圖,詢問用戶輸入。它工作正常,第一次,並給後出現以下錯誤:UIAlertController錯誤

代碼:

var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert) 
    func userBlogLinkEntryPopover() { 
     //  let alert = UIAlertView(title: "Enter Blog Link", message: nil, delegate: self, cancelButtonTitle: "Cancel") 
     alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      textField.placeholder = "Enter Blog URL!" 
     } 
     alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 
      if let tf = self.alert.textFields?.first as? UITextField{ 
       println(tf.text) 
      } 
     })) 
     alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
     presentViewController(alert, animated: true, completion: nil) 

    } 

錯誤:「UIAlertController只能用 UIAlertActionStyleCancel風格一個動作」

我認爲每次按下添加按鈕時都會嘗試添加操作,因此會出現錯誤。請糾正我,如果我錯了,也請建議工作。

感謝您的幫助。

+1

爲什麼您將'var alert'聲明從函數'userBlogLinkEntryPopover'移出?把它放回去。警報不需要是屬性。 – matt

回答

1

我發現,因爲alert是在函數外部聲明的,所以它保留了所有的操作,並且因爲它引發了異常。我更正了我的代碼,如下所示,它工作正常。

func userBlogLinkEntryPopover() {  
     var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert) 
     alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      textField.placeholder = "Enter Blog URL!" 
     } 
     alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 
      if let tf = alert.textFields?.first as? UITextField{ 
       println(tf.text) 
      } 
     })) 
     alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
     presentViewController(alert, animated: true, completion: nil) 

    } 

問候

0

你也可以得到這個,如果你做到這一點,請仔細閱讀看看你是否能看到錯誤

resendAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in 
     //cancel clicked 
     return 
    })) 

    resendAlert.addAction(UIAlertAction(title: "Check Server", style: .cancel, handler: { (action: UIAlertAction!) in 
     //check server clicked 
     return 
    })) 

注意--->的.cancel是使用TWICE它需要在第二個.default

resendAlert.addAction(UIAlertAction(title: "Check Server", style: .default, handler: { (action: UIAlertAction!) in 
     //check server clicked 
     return 
    })) 

正常工作

相關問題