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:

  1. inside windowcontrollerdidloadnib:

    you don't assign newly created button ivar function local variable (with same name ivar).

  2. why nothing happens inside refreshdevices

    init called before windowcontrollerdidloadnib:, ivar nil (and because of 1.). sending messages nil nothing.

solution:

  1. remove nspopupbutton * windowcontrollerdidloadnib: assign new button ivar , not function local variable.

  2. call refreshdevices @ end of windowcontrollerdidloadnib: or @ point know windowcontrollerdidloadnib: has been called , button not nil.


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

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -