我的目標是在主視圖的表格中創建一個新行,標題爲存儲在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
}
更新您的問題與關於崩潰的詳細信息,包括完整的錯誤信息,並指出哪一行導致崩潰。 – rmaddy 2014-10-18 05:05:23
添加了它,謝謝 – zwork 2014-10-18 05:12:58