代码之家  ›  专栏  ›  技术社区  ›  Will Zimmer

AlertController不保存文本保存时字段文本,仅编辑

  •  0
  • Will Zimmer  · 技术社区  · 10 年前

    我有一个带有textField的alertController。用户将数据输入textField并点击“set”。然后,它应该创建该项,并将输入的文本保存为我的属性。但是,在创建该项时,textField将传递零。直到项目重新打开并再次保存(提示alertController在textField中请求数据),它才会保存。为什么不第一次保存?

    按下保存按钮:

    @IBAction func saveButton(sender: AnyObject) {
        if (item?.slminqty == nil) {
        let alert = UIAlertController(title: "Minimun Qty.", message: "Please set minimun qty. for pantry.", preferredStyle: UIAlertControllerStyle.Alert)
    
            alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
            textField.placeholder = "Minimun Qty."
            textField.keyboardType = .NumbersAndPunctuation
            textField.clearButtonMode = UITextFieldViewMode.WhileEditing
        }
    
        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: {saveitem}()))
        alert.addAction(UIAlertAction(title: "Set", style: UIAlertActionStyle.Default, handler: {(action) -> Void in
            let textField = alert.textFields![0].text!
            self.item?.slminqty = textField
    
           self.saveitem(self)}))
    
            self.presentViewController(alert, animated: true, completion: nil)
    
        }else{
    
            if item != nil {
                edititems()
    
            } else {
                createitems()
            }
            print(item?.slminqty)
    
            dismissVC()
        }
    
        }
    

    保存功能:

    func saveitem(sender: AnyObject) {
    
        if item != nil {
            edititems()
    
        } else {
            createitems()
        }
        print(item?.slminqty)
    
        dismissVC()
    }
    

    创建函数:

    func createitems() {
    
        let entityDescription = NSEntityDescription.entityForName("List", inManagedObjectContext: moc)
    
        let item = List(entity: entityDescription!, insertIntoManagedObjectContext: moc)
    
        item.slitem = slitem.text
        item.sldesc = sldesc.text
        item.slqty = slqty.text
        item.slprice = slprice.text
        item.slist = true
        item.slcross = false
    
        if slitem.text == nil{
            createitems()
    
        }else{
            edititems()
        }
    
        do {
            try moc.save()
        } catch _ {
            return
        }
    }
    

    编辑功能:

    func edititems() {
        item?.slitem = slitem.text!
        item?.sldesc = sldesc.text!
        item?.slqty = slqty.text!
        item?.slprice = slprice.text!
    
        do {
            try moc.save()
        } catch {
            return
        }
    }
    

    如果创建和编辑都相同(slcross和list除外),为什么在创建项目时不保存数据?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Ismail    10 年前

    编辑 请看我的 pull reqest ,我对您的代码进行了一些更改。以及一些评论。

    我认为这方面的问题是:

     self.item?.slminqty = textField
    

    self.item 可能为零。你应该确保第一项不是零。

    如果为零,您可以尝试创建项。例如:

    if self.item == nil {
       //create item. 
       self.acreateItems()
       // after creating the item just test its value.
        print("item was nil so we just created it.\nIts value not is \(self.item)")
    }
    self.item?.slminqty = textField