2014-10-18 85 views
0

我的目標是在主視圖的表格中創建一個新行,標題爲存儲在namePrompt中的來自UIAlert的輸入。我將如何實現這一目標?UIAlert w/text field to table

當前,當我嘗試創建一個新行(使用iPad模擬器右上角的+按鈕,運行iOS 8)時,它會使應用程序崩潰。我不確定我做錯了什麼,並且任何幫助都值得讚賞。

代碼:

func insertNewObject(sender: AnyObject) { 
//prompt for kid name 
var namePrompt = UIAlertController(title: "Add Child", message: "Please enter child name", preferredStyle: UIAlertControllerStyle.Alert) 
namePrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)) 
namePrompt.addAction(UIAlertAction(title: "Add Child", style: UIAlertActionStyle.Default, handler: nil)) 
namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in 
textField.placeholder = "Johnny" 
}) 
self.presentViewController(namePrompt, animated: true, completion: nil) 

let childName: AnyObject = namePrompt.textFields![0] 

//add new object w/ above name 
objects.insertObject(childName, atIndex: 0) 
let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
} 

我覺得現在的問題從let childName: Any Object線產生的,和/或objects.insertObject線。

- 編輯 -

當我建立/運行該項目,沒有錯誤給我的代碼,但是當我嘗試添加一個新行,只要我按+按鈕,我從Thread 1得到0x1fbaff4: nopw %cs:(%eax,%eax)。這與行let object = objects[indexPath.row] as NSDate(在下面的代碼段中)行有關(上面的objects.insertObject行的默認值是NSDAte)。我將NSDate更改爲NSString,發生了同樣的情況。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    let object = objects[indexPath.row] as NSString 
    cell.textLabel?.text = object.description 
    return cell 
} 
+0

更新您的問題與關於崩潰的詳細信息,包括完整的錯誤信息,並指出哪一行導致崩潰。 – rmaddy 2014-10-18 05:05:23

+0

添加了它,謝謝 – zwork 2014-10-18 05:12:58

回答

1

你似乎認爲UIAlertController的textFields會在警告出現後立即被填充。這並沒有給用戶任何時間來填寫字段。

let childName = namePrompt.textFields![0]行(及其後面的行)放入其中一個按鈕操作中,並等待用戶在讀取textField之前輕觸該按鈕。

- 編輯 -

這裏是我的建議的修改你的代碼。我儘可能少地修改了代碼。請注意,有一條if語句來防止空的childNames,但它不會防止有人進入一堆空格。你可能應該讓這部分更健壯。

func insertNewObject(sender: AnyObject) { 
    //prompt for kid name 
    var namePrompt = UIAlertController(title: "Add Child", message: "Please enter child name", preferredStyle: UIAlertControllerStyle.Alert) 
    namePrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)) 
    namePrompt.addAction(UIAlertAction(title: "Add Child", style: UIAlertActionStyle.Default, handler: { (alertAction) in 
     let childName = (namePrompt.textFields![0] as UITextField).text as String 
     if childName != "" { 
      self.objects.insert(childName, atIndex: 0) 
      let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
      self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
     } 
    })) 
    namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in 
     textField.placeholder = "Johnny" 
    }) 
    self.presentViewController(namePrompt, animated: true, completion: nil) 

} 
+0

在'添加孩子'的'addAction'的'handler'點中? – zwork 2014-10-18 05:14:13

+0

這很有道理。在用戶點擊添加子按鈕之後,您可能不想讀取該值,對吧? – 2014-10-18 11:31:40

+0

我用上面的代碼替換了'insertNewObject'函數(謝謝),並且UIAlert正確地打開了,但是當我按下'Add Child'時,UIAlert消失了,鍵盤保持顯示,並且沒有添加新行。任何想法爲什麼? – zwork 2014-10-18 15:44:36

相關問題