UIBarButtonItem 的函数

Function for UIBarButtonItem

我正在使用 Core Data 并使用以下代码制作保存按钮:

    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žnica[25716:11689183] -[iKniz_nica.PridatViewController saveTapped]: unrecognized selector sent to instance 0x7ff5ab63ff50 2015-04-29 19:12:42.851 iKnižnica[25716:11689183] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[iKniz_nica.PridatViewController saveTapped]: unrecognized selector sent to instance 0x7ff5ab63ff50' * First throw call stack: ( 0 CoreFoundation 0x0000000100bccc65 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 0x0000000100b29cd8 _CF_forwarding_prep_0 + 120 5 UIKit
0x000000010146cda2 -[UIApplication sendAction:to:from:forEvent:] + 75 6 UIKit 0x000000010146cda2 -[UIApplication sendAction:to:from:forEvent:] + 75 7 UIKit 0x000000010157e54a -[UIControl _sendActionsForEvents:withEvent:] + 467 8 UIKit 0x000000010157d919 -[UIControl touchesEnded:withEvent:] + 522 9 UIKit 0x00000001014b9998 -[UIWindow _sendTouchesForEvent:] + 735 10 UIKit 0x00000001014ba2c2 -[UIWindow sendEvent:] + 682 11 UIKit
0x0000000101480581 -[UIApplication sendEvent:] + 246 12 UIKit
0x000000010148dd1c _UIApplicationHandleEventFromQueueEvent + 18265 13 UIKit 0x00000001014685dc _UIApplicationHandleEventQueue + 2066 14 CoreFoundation 0x0000000100b00431 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 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Ãånica
0x00000001005c6377 main + 135 21 libdyld.dylib
0x0000000102e8f145 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

我在这里发布了源代码:http://pastebin.com/M9AMakGr

将您的 setRightBarButtonItem 代码更改为以下代码:

self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "saveTapped:"), animated: true)

在swift中,对于选择器,您只需键入函数的名称即可!希望对你有帮助

问题不在按钮设置上,而是在功能上

而不是:

func saveTapped(){

应该是:

func saveTapped(sender:UIButton){