MAC application crash on button click event

Hi friends, I ran into a problem while creating a MAC application. I have two view controllers. when I click the button that fits into the first view controller application, replace the view using the code below

- (IBAction)gotoEmployeeView:(id)sender { delegate = (AppDelegate *)[[NSApplication sharedApplication]delegate]; secondViewController *employeeVC = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil]; [[delegate.window.contentView animator] replaceSubview:self.view with:secondVC.view]; secondVC.view.frame = ((NSView *)delegate.window.contentView).bounds; } 

I now have one button, and I set the click event for this button on the second screen. below id button click event

 - (IBAction)btnClicked:(id)sender { NSLog(@"clicked"); } 

and when I clicked on it, the application crashed. and get below error

 2013-08-12 14:33:46.989 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0 2013-08-12 14:33:46.991 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0 2013-08-12 14:33:46.994 Employee Register[1954:303] ( 0 CoreFoundation 0x00007fff8d76cf56 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff80e47d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff8d7f91be -[NSObject doesNotRecognizeSelector:] + 190 3 CoreFoundation 0x00007fff8d759e23 ___forwarding___ + 371 4 CoreFoundation 0x00007fff8d759c38 _CF_forwarding_prep_0 + 232 5 CoreFoundation 0x00007fff8d75c70d -[NSObject performSelector:withObject:] + 61 6 AppKit 0x00007fff8c5208ca -[NSApplication sendAction:to:from:] + 139 7 AppKit 0x00007fff8c5207fe -[NSControl sendAction:to:] + 88 8 AppKit 0x00007fff8c520729 -[NSCell _sendActionFrom:] + 137 9 AppKit 0x00007fff8c51fbec -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014 10 AppKit 0x00007fff8c59fb74 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489 11 AppKit 0x00007fff8c51e7f6 -[NSControl mouseDown:] + 786 12 AppKit 0x00007fff8c4e9c98 -[NSWindow sendEvent:] + 6306 13 AppKit 0x00007fff8c4833a5 -[NSApplication sendEvent:] + 5593 14 AppKit 0x00007fff8c419a0e -[NSApplication run] + 555 15 AppKit 0x00007fff8c695eac NSApplicationMain + 867 16 Employee Register 0x0000000100001342 main + 34 17 Employee Register 0x0000000100001314 start + 52 ) 
+4
source share
2 answers

It looks like you are sending a btnClicked message to an NSString object. Can you post the code you call [... bntClicked]?

The behavior you describe may mean that the viewController that implements the btnClicked method is released before the action is called or that the action in the interface builder is set incorrectly.

You must add the log statement to the dealloc viewController method that implements btnClicked, and make sure that it is not freed before the action is called. If you use ARC, make sure you have element data pointing to your controllers, so it is not freed.

You must also remove the ibaction association in the interface builder and do it again, making sure that you connect the button to the correct view controller.

0
source

Your gotoEmployeeView code looks good to me.
NOTE : the replaceSubview:with: method causes the removal of oldView.

Take a look at the sample project .

0
source

Source: https://habr.com/ru/post/1496524/


All Articles