2015-10-16 18 views
0

我需要一些幫助來從條形按鈕拉起自定義警報。目標是讓警報彈出,要求用戶輸入標題和說明。警報將有兩個文本字段,一個用於標題,另一個用於詳細信息,帶有「創建」和「取消」按鈕。Xcode7 UIAlertController獲取用戶數據

的代碼如下,但我發現兩個相同的錯誤在Xcode 7.0.1:

不能調用非功能型「[的UITextField]」的值

這個錯誤發生在這些行:

 let itemTitle = alert.textFields!(0) as UITextField! 
    let itemDetail = alert.textFields!(1) as UITextField! 

全碼:

@IBAction func addNewToDoListItem(sender: UIBarButtonItem) { 

    var alert = UIAlertController(title: "New ToDo Item", message: "Please enter a title and details for the new ToDo list item", preferredStyle: UIAlertControllerStyle.Alert) 

    var createItemAction = UIAlertAction(title: "Create", style: UIAlertActionStyle.Default) { (alertAction) -> Void in 

     let itemTitle = alert.textFields!(0) as UITextField! 
     let itemDetail = alert.textFields!(1) as UITextField! 

    } 

    var createCancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 

    alert.addTextFieldWithConfigurationHandler { (textField) -> 
     Void in 

     textField.placeholder = "Title" 

    } 

    alert.addTextFieldWithConfigurationHandler { (textField) -> 
     Void in 
      textField.placeholder = "Details" 

    } 

     alert.addAction(createItemAction) 
     alert.addAction(createCancelAction) 

     self.presentViewController(alert, animated: true, completion: nil) 

回答

0

嘗試使用括號代替鄰f圓括號:

alert.textFields![0]