找到你要的答案

Q:CoreData TextField - unexpectedly found nil

Q:CoreData TextField -意外发现零

I'm doing application with CoreData and textfields in tableview. In storyboard I have one textfield. textfield will multiply five times with different content in application. In navigation bar I have button that will save your written data. But when I press save button, application crash with error

IBOutlet is in the TableViewCell.swift. Code from image:

    @IBAction public func saveTapped(){

        if activeTextField.text.isEmpty {
            let alert = UIAlertView()
            alert.title = "Nevyplnené údaje"
            alert.message = "Musíš vyplniť všetky údaje o knihe."
            alert.addButtonWithTitle("Ok")
            alert.show()


        }
        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let obsah: NSManagedObjectContext = appDel.managedObjectContext!
        let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: obsah)
        var pridat = Model(entity: entity! , insertIntoManagedObjectContext: obsah)

        pridat.kniha = activeTextField.text
        pridat.autor = activeTextField.text
        pridat.rok = activeTextField.text
        pridat.vydavatelstvo = activeTextField.text
        pridat.strany = activeTextField.text

        obsah.
}

Here's full code:

http://pastebin.com/6LHqJW0w

Here's TableViewCell code:

http://pastebin.com/RAYTUcp6

I also tried

    let mfv = TableViewCell()

        if mfv.textField.text.isEmpty {
        let alert = UIAlertView()
        alert.title = "Nevyplnené údaje"
        alert.message = "Musíš vyplniť všetky údaje o knihe."
        alert.addButtonWithTitle("Ok")
        alert.show()
    }

我做在TableView CoreData和文本框的应用。在故事板我有一个文本框。将乘五次,应用不同的内容。在导航栏我有按钮,将保存您的书面数据。但当我按“保存”按钮时,应用程序会出现错误

在tableviewcell.swift IBOutlet。图像编码:

    @IBAction public func saveTapped(){

        if activeTextField.text.isEmpty {
            let alert = UIAlertView()
            alert.title = "Nevyplnené údaje"
            alert.message = "Musíš vyplniť všetky údaje o knihe."
            alert.addButtonWithTitle("Ok")
            alert.show()


        }
        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let obsah: NSManagedObjectContext = appDel.managedObjectContext!
        let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: obsah)
        var pridat = Model(entity: entity! , insertIntoManagedObjectContext: obsah)

        pridat.kniha = activeTextField.text
        pridat.autor = activeTextField.text
        pridat.rok = activeTextField.text
        pridat.vydavatelstvo = activeTextField.text
        pridat.strany = activeTextField.text

        obsah.
}

这里是全部代码:

http://pastebin.com/6lhqjw0w

这是tableviewcell代码:

http://pastebin.com/raytucp6

我也试过

    let mfv = TableViewCell()

        if mfv.textField.text.isEmpty {
        let alert = UIAlertView()
        alert.title = "Nevyplnené údaje"
        alert.message = "Musíš vyplniť všetky údaje o knihe."
        alert.addButtonWithTitle("Ok")
        alert.show()
    }
answer1: 回答1:

It looks like you are trying to set the value of activeTextField when the UITextField delegate methods are triggered. However, I can't see where you are setting your view controller to be the delegate for those textfields. I think you will need to add something to cellForRowAtIndexPath (or may be in the cell's configure method) to set the textField's delegate.

EDIT

I would amend your configure method to set the textField's delegate:

public func configure(placeholder: String, delegate : UITextFieldDelegate) {
        textField.placeholder = placeholder
        textField.accessibilityLabel = placeholder
        textField.delegate = delegate

    }

and amend the view controller likewise (wherever you call configure):

    cell!.configure("...", delegate:self)

END EDIT

I would also make activeTextField fully optional (not implicitly unwrapped), since it is entirely possible that it will be nil (if, for example, save is tapped before any textField has been edited). You should then use if let to unwrap the optional in the saveTapped method.

它看起来像你想的activetextfield值时的UITextField委托方法引发。但是,我不能看到你设置你的视图控制器为代表的文本框。我想你需要添加什么cellForRowAtIndexPath(或可能在细胞的配置方法)来设置文本的代表。

编辑

我会修改你的配置方法来设置文本的代表:

public func configure(placeholder: String, delegate : UITextFieldDelegate) {
        textField.placeholder = placeholder
        textField.accessibilityLabel = placeholder
        textField.delegate = delegate

    }

并修改视图控制器同样(无论你叫配置):

    cell!.configure("...", delegate:self)

END 编辑

我也会使activetextfield完全可选(不含蓄地解开),因为它是完全可能的,这将是零(如果,例如,保存在任何文本挖掘已经编辑)。然后你应该使用如果让打开可选在savetapped方法。

ios  xcode  swift  core-data  null