Function for UIBarButtonItem - ios

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

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

Related

Swift Swipe gesture recognizer crashes program [duplicate]

This question already has answers here:
UISwipeGestureRecognizer throw unrecognised selector sent error
(2 answers)
Closed 5 years ago.
Hello I added a swift gesture recognizer and it keeps crashing my program
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
func handleSwipes(sender: UISwipeGestureRecognizer) {
if (sender.direction == .left ) {
headerLabel.text = "Zodiac Yearly"
}
if (sender.direction == .right ) {
headerLabel.text = "Characteristics"
}
}
This is the error I am getting
2017-09-08 20:17:33.355 Views[40942:2309262] -[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90
2017-09-08 20:17:33.361 Views[40942:2309262] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111f40b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010f1e6141 objc_exception_throw + 48
2 CoreFoundation 0x0000000111fb0134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000111ec7840 ___forwarding___ + 1024
4 CoreFoundation 0x0000000111ec73b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010fbecf59 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
6 UIKit 0x000000010fbf4d57 _UIGestureRecognizerSendTargetActions + 109
7 UIKit 0x000000010fbf270b _UIGestureRecognizerSendActions + 225
8 UIKit 0x000000010fbf19ce -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 981
9 UIKit 0x000000010fbde152 _UIGestureEnvironmentUpdate + 1219
10 UIKit 0x000000010fbddc43 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
11 UIKit 0x000000010fbdce0a -[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
12 UIKit 0x000000010f728eea -[UIWindow sendEvent:] + 4092
13 UIKit 0x000000010f6d5a84 -[UIApplication sendEvent:] + 352
14 UIKit 0x000000010feb95d4 __dispatchPreprocessedEventFromEventQueue + 2926
15 UIKit 0x000000010feb1532 __handleEventQueue + 1122
16 CoreFoundation 0x0000000111ee6c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x0000000111ecc0cf __CFRunLoopDoSources0 + 527
18 CoreFoundation 0x0000000111ecb5ff __CFRunLoopRun + 911
19 CoreFoundation 0x0000000111ecb016 CFRunLoopRunSpecific + 406
20 GraphicsServices 0x0000000113ed7a24 GSEventRunModal + 62
21 UIKit 0x000000010f6b8134 UIApplicationMain + 159
22 Views 0x000000010ec06a67 main + 55
23 libdyld.dylib 0x0000000112f6c65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(sender:)))
#objc func handleSwipes(sender: UISwipeGestureRecognizer) {
// ...
}
This should work in Swift 3 and 4.

Error when I return on TableViewController (swift)

I'm a new Swift programmer. I'm building my first app with TableViewController + NavigationController.
In my app, I can see this:
If I click on Plus button I can see this View:
Then I compile all field and then I click on OK button. But I have this error:
2017-04-20 16:23:48.158 ArduinoHomeKit_bis[3238:795882] -[LuciKit id]: unrecognized selector sent to instance 0x61000026a700
2017-04-20 16:23:48.162 ArduinoHomeKit_bis[3238:795882] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LuciKit id]: unrecognized selector sent to instance 0x61000026a700'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107745b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001045c0141 objc_exception_throw + 48
2 CoreFoundation 0x00000001077b5134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00000001076cc840 ___forwarding___ + 1024
4 CoreFoundation 0x00000001076cc3b8 _CF_forwarding_prep_0 + 120
5 ArduinoHomeKit_bis 0x0000000103fc0603 _TFC18ArduinoHomeKit_bis28ListaLuciTableViewController14tornaAllaListafCSo17UIStoryboardSegueT_ + 435
6 ArduinoHomeKit_bis 0x0000000103fc098a _TToFC18ArduinoHomeKit_bis28ListaLuciTableViewController14tornaAllaListafCSo17UIStoryboardSegueT_ + 58
7 UIKit 0x0000000105903bbf -[UIStoryboardUnwindSegueTemplate _performWithDestinationViewController:sender:] + 214
8 UIKit 0x0000000105903ab6 -[UIStoryboardUnwindSegueTemplate _perform:] + 83
9 UIKit 0x000000010566ea1f -[UIStoryboardSegueTemplate perform:] + 157
10 UIKit 0x0000000104ea8d22 -[UIApplication sendAction:to:from:forEvent:] + 83
11 UIKit 0x00000001052e35c7 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149
12 UIKit 0x0000000104ea8d22 -[UIApplication sendAction:to:from:forEvent:] + 83
13 UIKit 0x000000010502d25c -[UIControl sendAction:to:forEvent:] + 67
14 UIKit 0x000000010502d577 -[UIControl _sendActionsForEvents:withEvent:] + 450
15 UIKit 0x000000010502d6eb -[UIControl _sendActionsForEvents:withEvent:] + 822
16 UIKit 0x000000010502c4b2 -[UIControl touchesEnded:withEvent:] + 618
17 UIKit 0x0000000104f1649a -[UIWindow _sendTouchesForEvent:] + 2707
18 UIKit 0x0000000104f17bb0 -[UIWindow sendEvent:] + 4114
19 UIKit 0x0000000104ec47b0 -[UIApplication sendEvent:] + 352
20 UIKit 0x00000001056a7adc __dispatchPreprocessedEventFromEventQueue + 2926
21 UIKit 0x000000010569fa3a __handleEventQueue + 1122
22 CoreFoundation 0x00000001076ebc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
23 CoreFoundation 0x00000001076d10cf __CFRunLoopDoSources0 + 527
24 CoreFoundation 0x00000001076d05ff __CFRunLoopRun + 911
25 CoreFoundation 0x00000001076d0016 CFRunLoopRunSpecific + 406
26 GraphicsServices 0x00000001096fca24 GSEventRunModal + 62
27 UIKit 0x0000000104ea70d4 UIApplicationMain + 159
28 ArduinoHomeKit_bis 0x0000000103fc3ce7 main + 55
29 libdyld.dylib 0x00000001086e565d start + 1
30 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
After click OK this is the code that I try to execute:
#IBAction func tornaAllaLista(_ segue: UIStoryboardSegue){
do {
var vistaDettaglio: AggiungiLuceViewController = segue.source as! AggiungiLuceViewController
if(vistaDettaglio.nuovaLuce != nil && vistaDettaglio.nuovaLuce?.id == 0){
self.listaLuci.append(vistaDettaglio.nuovaLuce!)
self.tabella.reloadData()
}else{
}
} catch let errore {
print("[CDC] problema tornaAllaLista")
print(" Stampo l'errore: \n \(errore) \n")
}
}
EDIT
This is the code that I execute when I click on OK button:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//se il pulsante cliccato è diverso da OK torno indietro
if sender as? NSObject != self.buttonOK{
return
}
let nomeLuce = self.textNomeLuce.text!
let pinArduino = Int16(self.textPinArduino.text!)
let tipoLuce = self.textTipoLuce.text!
if(nomeLuce.characters.count>0){
//ho inserito almeno un carattere
LuciKitCoreDataController.shared.addLuce(descrizione: nomeLuce, pin_arduino: Int(pinArduino!))
self.nuovaLuce = LuciKit()
self.nuovaLuce?.descrizione = nomeLuce
self.nuovaLuce?.pin_arduino = pinArduino!
self.nuovaLuce?.id = 3
}else{
let alert = UIAlertController(title:"Attenzione", message: "Inserire un nome per la Luce", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated:true, completion: nil)
}
}
If I try to execute this line code:
self.nuovaLuce?.descrizione = nomeLuce
I have an error
Pretty sure that this is the offending line:
vistaDettaglio.nuovaLuce?.id == 0
Object stored under novaLuce doesn't contain a method called -id, as it seems to be.

iOS 9 Local Notifications using Swift 2

I have the following code
#IBAction func triggerNotif(sender: UIButton) {
let dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015
dateComp.month = 11
dateComp.day = 12
dateComp.hour = 21
dateComp.minute = 30
dateComp.timeZone = NSTimeZone.systemTimeZone()
let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let date:NSDate = calender.dateFromComponents(dateComp)!
let notification:UILocalNotification = UILocalNotification()
notification.alertBody = "body of notification"
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.Day
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Basically, I'm trying to trigger a notification that repeats every day using a UIButton. I referred to many other questions on this site to form up this code, and it builds successfully.
But when I click the button, I'm faced with this:
2015-11-12 18:30:17.025 Application Name[95723:485982] -[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70
2015-11-12 18:30:17.035 Application Name[95723:485982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d293f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010efb7deb objc_exception_throw + 48
2 CoreFoundation 0x000000010d29c56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010d1e9eea ___forwarding___ + 970
4 CoreFoundation 0x000000010d1e9a98 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010dab1e91 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x000000010dc1d4d8 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000000010dc1d7a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
8 UIKit 0x000000010dc1c8d4 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x000000010db1fed1 -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x000000010db20c06 -[UIWindow sendEvent:] + 865
11 UIKit 0x000000010dad02fa -[UIApplication sendEvent:] + 263
12 UIKit 0x000000010daaaabf _UIApplicationHandleEventQueue + 6844
13 CoreFoundation 0x000000010d1c0011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010d1b5f3c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x000000010d1b53f3 __CFRunLoopRun + 867
16 CoreFoundation 0x000000010d1b4e08 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x0000000111887ad2 GSEventRunModal + 161
18 UIKit 0x000000010dab030d UIApplicationMain + 171
19 Application Name 0x000000010d0a8b6d main + 109
20 libdyld.dylib 0x000000010fabf92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
What's wrong?
you set triggerNotifBtn as function name but your function name is triggerNotif

terminating with uncaught exception of type NSException - Xcode

I'm using Xcode 6.4 and get the following error when trying to use a use a back button I hard coded:
2015-07-01 22:41:47.082 oby[14979:280903] -[oby.PhotoViewController popView]: unrecognized selector sent to instance 0x7f8112f24d90
2015-07-01 22:41:47.103 oby[14979:280903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[oby.PhotoViewController popView]: unrecognized selector sent to instance 0x7f8112f24d90'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105b92c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001076fdbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000105b9a0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000105af013c ___forwarding___ + 988
4 CoreFoundation 0x0000000105aefcd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000106432d62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010654450a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001065438d9 -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010647f958 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x0000000106480282 -[UIWindow sendEvent:] + 682
10 UIKit 0x0000000106446541 -[UIApplication sendEvent:] + 246
11 UIKit 0x0000000106453cdc _UIApplicationHandleEventFromQueueEvent + 18265
12 UIKit 0x000000010642e59c _UIApplicationHandleEventQueue + 2066
13 CoreFoundation 0x0000000105ac6431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000105abc2fd __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x0000000105abb934 __CFRunLoopRun + 868
16 CoreFoundation 0x0000000105abb366 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x000000010a676a3e GSEventRunModal + 161
18 UIKit 0x00000001064318c0 UIApplicationMain + 1282
19 oby 0x00000001053502b7 main + 135
20 libdyld.dylib 0x0000000107e33145 start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Button:
let btn = UINavButton(title: "Back", direction: UIButtonDirection.Left, parentView: self.view)
btn.addTarget(self, action: "popView", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
Could someone help me fix this please?
Thank you!
Something like this
btn.addTarget(self, action: "backAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func backAction() {
self.navigationController?.popViewControllerAnimated(true)
}

segmented control - unrecognized selector sent to instance

I want to use sender to get the value from segmented control, but I am getting the unrecognized selector sent to instance error. The segmented control was added within a view. I've tried removing the semicolon and/or sender and none of the prior posts I've found seem to help. Any ideas?
Swift Code
var segCntrl = UISegmentedControl(items: ["Yes","No"])
segCntrl.tag = 100
segCntrl.center = CGPointMake(qView.bounds.width/2, qView.bounds.minY+40)
self.qView.addSubview(segCntrl)
func segmentorSwitch(sender: AnyObject) {
if(sender.selectedSegmentIndex == 1) {
println("Yes selected")
}
else if(sender.selectedSegmentIndex == 2) {
println("No selected")
}
}
segCntrl.addTarget(self, action: "segmentorSwitch:", forControlEvents: UIControlEvents.ValueChanged)
Error code:
2015-07-07 11:46:24.482 myApp [75641:1855377] -[[myApp.SegViewControllerr segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450
2015-07-07 11:46:24.489 myApp[75641:1855377] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myApp.SegViewController segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450'
* First throw call stack:
(
0 CoreFoundation 0x00000001060dcc65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010838abb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001060e40ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010603a13c ___forwarding_ + 988
4 CoreFoundation 0x0000000106039cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001070bfd62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001071d150a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010724dfba -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570
8 UIKit 0x000000010724ffbf -[UISegmentedControl touchesEnded:withEvent:] + 143
9 UIKit 0x000000010710c958 -[UIWindow _sendTouchesForEvent:] + 735
10 UIKit 0x000000010710d282 -[UIWindow sendEvent:] + 682
11 UIKit 0x00000001070d3541 -[UIApplication sendEvent:] + 246
12 UIKit 0x00000001070e0cdc _UIApplicationHandleEventFromQueueEvent + 18265
13 UIKit 0x00000001070bb59c _UIApplicationHandleEventQueue + 2066
14 CoreFoundation 0x0000000106010431 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
15 CoreFoundation 0x00000001060062fd __CFRunLoopDoSources0 + 269
16 CoreFoundation 0x0000000106005934 __CFRunLoopRun + 868
17 CoreFoundation 0x0000000106005366 CFRunLoopRunSpecific + 470
18 GraphicsServices 0x000000010cc5ba3e GSEventRunModal + 161
19 UIKit 0x00000001070be8c0 UIApplicationMain + 1282
20 myApp 0x0000000104d9a0c7 main + 135
21 libdyld.dylib 0x0000000109458145 start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Looks like segmentorSwitch: is implemented inside another method. Define it outside the method in which your code is contained, and this should be fixed.

Resources