objective c - Programmatically create NSPopupButton and add items to list -
i've been able programmatically create nspopupbutton , add window, , can add items list same method, i'd figure out how can add items method.
here's have far works:
// in .h file: @interface avrecorderdocument : nsdocument { @private nspopupbutton *button; } @property (assign) iboutlet nswindow *mainwindow;
// in .m file: @implementation avrecorderdocument @synthesize mainwindow; - (void)windowcontrollerdidloadnib:(nswindowcontroller *) acontroller { nsview *superview = [mainwindow contentview]; nsrect frame = nsmakerect(10,10,149,22); nspopupbutton *button = [[nspopupbutton alloc] initwithframe:frame]; [superview addsubview:button]; [button release]; } - (void)refreshdevices { // i'd add items popupbutton here: // [button additemwithtitle: @"item 1"]; } @end
up in refreshdevices don't compiler error, nothing gets added popupbutton. method refreshdevices called -(id)init. i've tried putting code inside windowcontrollerdidloadnib @ top of init section, won't create popupbutton there.
there 2 problems code:
inside
windowcontrollerdidloadnib:
you don't assign newly created button ivar function local variable (with same name ivar).
why nothing happens inside
refreshdevices
init
called beforewindowcontrollerdidloadnib:
, ivarnil
(and because of 1.). sending messagesnil
nothing.
solution:
remove
nspopupbutton *
windowcontrollerdidloadnib:
assign new button ivar , not function local variable.call
refreshdevices
@ end ofwindowcontrollerdidloadnib:
or @ point knowwindowcontrollerdidloadnib:
has been called , button notnil
.
edit:
you should keep in mind moment remove button superview deallocated because release after creation.
the moment deallocated button
ivar points invalid/deallocated object leads undefined behaviour when used in state.
i'd advise release
button inside dealloc
can sure have valid object throughout whole lifetime of document object.
but nonetheless don't know exact use case might require design.
Comments
Post a Comment