objective c - Cocoa: Button Control in secondary .xib (loaded from MainWindowController) fails to invoke its Action -
a simple test application using xcode 7.3 under macos 10.10 using objective c.
appdelegate.h @property mainwindowcontroller *mymainwindowcontroller; appdelegate.m - (void)applicationdidfinishlaunching:(nsnotification *)anotification { _mymainwindowcontroller = [[mainwindowcontroller alloc] initwithwindownibname:@"mainwindowcontroller"]; [_mymainwindowcontroller showwindow:self]; }
the mainwindowcontroller.xib mainwindow main view
mainwindowcontroller.h @property iboutlet nsview * mainview; //hooked view in mainwindowcontroller.xib mainwindowcontroller.m - (void)windowdidload { [super windowdidload]; myxib* vc = [[myxib alloc] initwithnibname:nil bundle:nil]; [_mainview addsubview:[vc view] ]; }
myxib created in xcode subclass of nsviewcontroller. has button object, in view.
myxib.h
@interface myxib : nsviewcontroller - (ibaction)buttonpushed:(id)sender;
myxib.m
- (void)viewdidload { [super viewdidload]; } - (ibaction)buttonpushed:(id)sender{ nslog(@"buttonpushed"); }
the action thebutton connected buttonpushed.
when running application, user interface displayed expected. when button clicked, program crashes without ever invoking buttonpushed method. debug window shows stack crawl ending "[nsurl buttonpushed:]: unrecognized selector sent instance.."
i have put in calls viewdidload, awakefromnib, , init. break points in these calls seem show things behaving logically according documentation.
thanks willeke! here fix maintain reference:
#import "mainwindowcontroller.h" @interface mainwindowcontroller () @property iboutlet nsview * mainview; @property myxib* vc; // maintain reference! @end @implementation mainwindowcontroller - (void)windowdidload { [super windowdidload]; //create nsviewcontroller , use load xib _vc = [myxib alloc]; _vc = [_vc initwithnibname:nil bundle:nil]; [_mainview addsubview:[_vc view] ]; } @end
Comments
Post a Comment