objective c - How to initialize main window objects from custom class during program start? -
i have main window couple of popupbuttons. want clear them, load lists method in custom class. i've got view controller working , know method in custom class (newrequest) working because added nslog command print "test" when method executes. in appdelegate i'm calling method via: [polyapprequest newrequest];. said, know method executing. why can't removeallitems popupbutton custom class method? keith
i read should use nswindowcontroller manage window. see here:
adding views or windows mainwindow
then if window gets complicated enough, nswindowcontroller can employ various nsviewcontrollers manage parts of window.
in case, used nswindowcontroller in answer.
the image below shows outlet's file's owner, mainwindowcontroller
:
i created mainwindowcontroller .h/.m in xcode6.2
by:
- selecting file>new>file>os x - source - cocoa class
- selecting nswindowcontroller
subclass of:
- checking
also create .xib file user interface
then deleted window--not menu--in default mainmenu.xib, , changed name of mainwindowcontroller.xib, created steps above, mainwindow.xib.
the following code works me (but i'm cocoa beginner!):
// // appdelegate.m // popupbuttons #import "appdelegate.h" #import "mainwindowcontroller.h" @interface appdelegate () @property(strong) mainwindowcontroller* mainwindowctrl; @end @implementation appdelegate - (void)applicationdidfinishlaunching:(nsnotification *)anotification { // insert code here initialize application [self setmainwindowctrl:[[mainwindowcontroller alloc] init]]; [[self mainwindowctrl] showwindow:nil]; } - (void)applicationwillterminate:(nsnotification *)anotification { // insert code here tear down application } @end
...
// // mainwindowcontroller.m // popupbuttons // #import "mainwindowcontroller.h" #import "mydata.h" @interface mainwindowcontroller () @property(strong) mydata* data; @property(weak) iboutlet nspopupbutton* namepopup; @property(weak) iboutlet nspopupbutton* agepopup; @end @implementation mainwindowcontroller -(id)init { if (self = [super initwithwindownibname:@"mainwindow"]) { _data = [[mydata alloc] init]; //get data popups } return self; } - (void)windowdidload { [super windowdidload]; // implement method handle initialization after window controller's window has been loaded nib file. [[self namepopup] removeallitems]; [[self namepopup] additemswithtitles:[[self data] drinks]]; [[self agepopup] removeallitems]; [[self agepopup] additemswithtitles:[[self data] extras]]; } @end
...
// // mydata.h // popupbuttons // #import <foundation/foundation.h> @interface mydata : nsobject @property nsarray* drinks; @property nsarray* extras; @end
...
// // mydata.m // popupbuttons // #import "mydata.h" @implementation mydata - (id)init { if (self = [super init]) { _drinks = @[@"coffee", @"tea"]; _extras = @[@"milk", @"sugar", @"honey"]; } return self; } @end
i hope helps. if need more screenshots, let me know.
edit1:
i think see asking about. although don't think approach, if change code this:
// // mydata.h // popupbuttons // #import <cocoa/cocoa.h> @interface mydata : nsobject @property (copy) nsarray* drinks; @property (copy) nsarray* extras; -(void)newrequest; @end
...
// // mydata.m // popupbuttons // #import "mydata.h" @interface mydata() @property (weak) iboutlet nspopupbutton* drinkspopup; @property (weak) iboutlet nspopupbutton* extraspopup; @end @implementation mydata - (id)init { if (self = [super init]) { _drinks = @[@"coffee", @"tea"]; _extras = @[@"milk", @"sugar", @"honey"]; } return self; } -(void)newrequest { [[self drinkspopup] removeallitems]; [[self drinkspopup] additemswithtitles:[self drinks]]; [[self extraspopup] removeallitems]; [[self extraspopup] additemswithtitles:[self extras]]; } @end
i unable populate nspopupbuttons. did:
i dragged object object library dock in ib, , in identity inspector, changed object's class mydata.
then clicked on connections inspector, , 2 instance variables in mydata, drinkspopup , extraspopup, listed in outlets.
i dragged outlets respective nspopupbuttons.
i guess assumed, you, when program ran, nspopupbuttons assigned instance variables drinkspopup , extraspopup--but doesn't seem case. according apple docs, should able that:
an application typically sets outlet connections between custom controller objects , objects on user interface, can made between objects can represented instances in interface builder,...
edit2:
i able pass nspopupbuttons mainwindowcontroller newrequest method, , can use nspopupbuttons inside newrequest populate data.
edit3:
i know method in custom class (newrequest) working because added nslog command print "test" when method executes.
but happens when log variables point nspopupbuttons? code in edit1
, null variables, means nspopupbuttons never got assigned variables.
edit4:
if add awakefromnib
method mydata, , inside awakefromnib log nspopupbutton variables code in edit1, non null values. tells me mainwindowcontroller's windowdidload method executing before mydata's awakefromnib method, , therefore cannot call newrequest inside mainwindowcontroller's windowdidload method because mydata has not been initialized.
edit5:
okay, got code in edit1 work. apple docs this:
about top-level objects
when program loads nib file, cocoa recreates entire graph of objects created in xcode. object graph includes of windows, views, controls, cells, menus, , custom objects found in nib file. top-level objects subset of these objects not have parent object [in ib]. top-level objects typically include windows, menubars, , custom controller objects add nib file [like mydata object]. (objects such file’s owner, first responder, , application placeholder objects , not considered top-level objects.)
typically, use outlets in file’s owner object store references top-level objects of nib file. if not use outlets, however, can retrieve top-level objects nib-loading routines directly. should keep pointer these objects somewhere because application responsible releasing them when done using them. more information nib object behavior @ load time, see managing lifetimes of objects nib files.
in accordance bolded line above, changed declaration in mainwindowcontroller.m:
@interface mainwindowcontroller () @property(strong) mydata* data; ... @end
to this:
@interface mainwindowcontroller () @property(strong) iboutlet mydata* data; ... @end
then, in ib dragged connection mainwindowcontroller data outlet mydata object(the object had dragged out of object library , onto doc).
i guess causes mydata unarchive .xib file , initialize before mainwindowcontroller.
Comments
Post a Comment