我正在用TableView中的CoreData和textfields做应用程序。在情节提要中,我有一个文本字段。文本字段将与应用程序中的不同内容相乘五次。在导航栏中,我有一个按钮,它将保存您的书面数据。但是,当我按保存按钮时,应用程序因错误而崩溃
IBOutlet在TableViewCell.swift中。图片中的代码:
@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.
}
这是完整的代码:
这是TableViewCell代码:
我也试过了
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()
}
看来您正在尝试设置委托方法被触发activeTextField
时的值UITextField
。但是,我看不到您将视图控制器设置delegate
为那些文本字段的位置。我认为您将需要添加一些内容cellForRowAtIndexPath
(或可能在单元格的configure
方法中)以设置textField的delegate
。
编辑
我会修改您的configure
方法来设置textField的委托:
public func configure(placeholder: String, delegate : UITextFieldDelegate) {
textField.placeholder = placeholder
textField.accessibilityLabel = placeholder
textField.delegate = delegate
}
并同样修改视图控制器(无论您在何处调用configure
):
cell!.configure("...", delegate:self)
结束编辑
我还将设置activeTextField
完全可选的选项(而不是隐式展开),因为它完全有可能为零(例如,如果在编辑任何textField之前点击了save)。然后,您应该使用if let
来解开saveTapped
方法中的可选参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句