简体   繁体   English

AlertController不会在保存时保存textField文本,只能编辑

[英]AlertController doesn't save textField text on save, only edit

I have an alertController with a textField. 我有一个带有textField的alertController。 The user enters their data into the textField and hits "set". 用户将其数据输入到textField中,然后单击“设置”。 It should then create the item and save the text entered as my attribute it's set to. 然后,它应该创建该项目,并将输入的文本保存为设置为我的属性。 However, upon creating the item the textField passes nil. 但是,创建项目时,textField传递nil。 It's not saved until the item is reopened and saved again (prompting the alertController to request data in the textField). 在重新打开并再次保存该项目之前,不会保存它(提示alertController在textField中请求数据)。 Why is it not saving it the first time? 为什么第一次不保存呢?

saveButton pressed: 按下saveButton:

@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()
    }

    }

Save function: 保存功能:

func saveitem(sender: AnyObject) {

    if item != nil {
        edititems()

    } else {
        createitems()
    }
    print(item?.slminqty)

    dismissVC()
}

Create function: 创建函数:

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
    }
}

Edit function: 编辑功能:

func edititems() {
    item?.slitem = slitem.text!
    item?.sldesc = sldesc.text!
    item?.slqty = slqty.text!
    item?.slprice = slprice.text!

    do {
        try moc.save()
    } catch {
        return
    }
}

If both of the create and edit are the same (with the exception of slcross and slist) why won't it save the data when the item is created? 如果创建和编辑都相同(除了slcross和slist),为什么在创建项目时不保存数据?

Edit please see my pull reqest , I have made some changes to your code. 编辑,请参阅我的请求 ,我对您的代码进行了一些更改。 along with some comments. 以及一些评论。

I think the problem in this line : 我认为这条线的问题:

 self.item?.slminqty = textField

self.item might be nil. self.item可能为零。 you should make sure first item is not nil. 您应该确保第一项不是零。

you may try to create item if this is nil. 您可以尝试创建项目(如果为零)。 like: 喜欢:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM