我正在使用Core数据,并使用以下代码创建了一个保存按钮:
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: Selector("saveTapped")), animated: true)保存功能:
func saveTapped(){
if (cell!.textField.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 = cell!.textField.text
pridat.autor = cell!.textField.text
pridat.rok = cell!.textField.text
pridat.vydavatelstvo = cell!.textField.text
pridat.strany = cell!.textField.text
obsah.save(nil)
}这是我正在犯的错误:
2015-04-29 19:12:42.762 iKnižnica25716:11689183 -iKniz_nica.PridatViewController saveTapped:未被识别的选择器发送到实例0x7ff5ab63ff50 2015-04-29 19:42.851 iKnižnica25716:11689183 *终止应用程序由于未登录异常'NSInvalidArgumentException',原因:'-iKniz_nica.PridatViewController saveTapped:发送给实例0x7ff5ab63ff50‘*第一次抛出调用堆栈:(0 CoreFoundation 0x0000100bccc65 exceptionPreprocess + 165 1 libobjc.A.dylib ) 0x0000000102737bb7 objc_exception_throw + 45 2 CoreFoundation 0x0000000100bd40ad -NSObject(NSObject) doesNotRecognizeSelector:+ 205 3 CoreFoundation 0x0000000100b2a13c ___forwarding_ + 988 4 CoreFoundation 0x0000000100b29c8 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010146cda2 -UIApplication sendAction:to:from:forEvent:+756 UIKit 0x00000010146cda2 -UIApplication sendAction:to:forEvent:+ 75 7 UIKit 0x000000010157e54a -UIControl _sendActionsForEvents:withEvent:+ 467 8 UIKit 0x000000010157d919 -UIControl接触点:withEvent:+ 522 9 UIKit-UIWindow _sendTouchesForEvent:+ 735 10 UIKit 0x00000001014ba2c2 -UIWindow sendEvent:+ 682 11 UIKit -UIApplication sendEvent:+ 246 12 UIKit 0x000000010148dd1c _UIApplicationHandleEventFromQueueEvent + 18265 13 UIKit 0x00000001014685dc _UIApplicationHandleEventQueue + 2066 14 CoreFoundation 0x0000000100b00431 _UIApplicationHandleEventQueue+ 17 15 CoreFoundation 0x0000000100af62fd __CFRunLoopDoSources0 + 269 16 CoreFoundation 0x0000000100af5934 __CFRunLoopRun + 868 17 CoreFoundation 0x0000000100af5366 CFRunLoopRunSpecific + 470 18 GraphicsServices 0x0000000104bb3a3e GSEventRunModal + 161 19 UIKit 0x000000010146b900 UIApplicationMain + 1282 20 iKniz 0x00000001005c6377 main + 135 21 libdyld.dylib 0x0000000102e8f145Start+1) libc++abi.dylib:以NSException (lldb)类型的完全例外终止
我在这里发布了源代码:http://pastebin.com/M9AMakGr
发布于 2015-05-12 21:13:33
问题不在于按钮设置,而在于功能。
而不是:
func saveTapped(){应:
func saveTapped(sender:UIButton){发布于 2015-04-29 17:42:38
将setRightBarButtonItem代码更改为以下代码:
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "saveTapped:"), animated: true)对于选择器,您只需键入函数的名称,就可以了!希望它能帮上忙
https://stackoverflow.com/questions/29950423
复制相似问题