1
votes

I'm using Core Data and I'm making a save button with this code:

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

I posted the source code here: http://pastebin.com/M9AMakGr

2
Try change Selector("saveTapped") to Selector("saveTapped:")cmashinho
tried... Same problemdafinoo
@dafinaoo read it. Maybe Selector(saveTapped:)cmashinho
In Swift u can't use Selector(saveTapped:) without "dafinoo
ohhh.. Just use only "saveTapped:" linkcmashinho

2 Answers

1
votes

Problem is not in the button settings, but in function

Instead of:

func saveTapped(){

Should be:

func saveTapped(sender:UIButton){
0
votes

Change your setRightBarButtonItem code to below code:

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