简体   繁体   English

UIBarButtonItem的函数

[英]Function for UIBarButtonItem

I'm using Core Data and I'm making a save button with this code: 我正在使用Core Data,并使用以下代码创建了一个保存按钮:

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

The save function: 保存功能:

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

This is the error I'm getting: 这是我得到的错误:

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 2015-04-29 19:12:42.762iKnižnica[25716:11689183]-[iKniz_nica.PridatViewController saveTapped]:无法识别的选择器已发送到实例0x7ff5ab63ff50 2015-04-29 19:12:42.851iKnižnica[25716:11689183] *终止应用程序到期到未捕获的异常'NSInvalidArgumentException',原因:'-[iKniz_nica.PridatViewController saveTapped]:无法识别的选择器发送到实例0x7ff5ab63ff50'*第一个调用堆栈:(0 CoreFoundation 0x0000000100bccc65 异常预处理+ 165 1 libobjc.A.dylib
0x0000000102737bb7 objc_exception_throw + 45 2 CoreFoundation 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 0x0000000100bd40ad-[NSObject(NSObject)didNotRecognizeSelector:] + 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 0x000000010146cda2-[UIApplication sendAction:to:from:forEvent:] + 75 6 UIKit 0x000000010146cda2-[UIApplication sendAction:to:from: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 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 0x000000010148dd1c _UIApplicationHandleEventFromQueueEvent + 18265 13 UIKit 0x00000001014685dc _UIApplicationHandleEventQueue + 2066 14 CoreFoundation 0x0000000100b00431 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15 CoreFoundation 0x0000000100af62fDoop 17CCoreRuns 0Do_0000000100af62fd4 __0Run0Source100af62fd___
0x0000000100af5366 CFRunLoopRunSpecific + 470 18 GraphicsServices 0x0000000100af5366 CFRunLoopRunSpecific + 470 18图形服务
0x0000000104bb3a3e GSEventRunModal + 161 19 UIKit 0x0000000104bb3a3e GSEventRunModal + 161 19 UIKit
0x000000010146b900 UIApplicationMain + 1282 20 iKnizÃånica 0x000000010146b900 UIApplicationMain + 1282 20iKnizÃånica
0x00000001005c6377 main + 135 21 libdyld.dylib 0x00000001005c6377主+ 135 21 libdyld.dylib
0x0000000102e8f145 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 0x0000000102e8f145 start +1)libc ++ abi.dylib:以类型为NSException(lldb)的未捕获异常终止

I posted the source code here: http://pastebin.com/M9AMakGr 我在这里发布了源代码: http : //pastebin.com/M9AMakGr

Problem is not in the button settings, but in function 问题不在于按钮设置,而在于功能

Instead of: 代替:

func saveTapped(){

Should be: 应该:

func saveTapped(sender:UIButton){

Change your setRightBarButtonItem code to below code: 将您的setRightBarButtonItem代码更改为以下代码:

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

In swift, for selector you just need to type the name of the function, that's it! 快速地,对于选择器,您只需要键入函数的名称即可! Hope it helps 希望能帮助到你

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

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