objective c - Correct method to present a different NSViewController in NSWindow -
i developing app single nswindow
, clicking button inside window present nsviewcontroller
, , button exists in controller present different nsviewcontroller
. know how swap out views in window, ran issue trying multiple view controllers. have resolved issue, don't believe accomplishing behavior in appropriate way.
i defined method in appdelegate
:
- (void)displayviewcontroller:(nsviewcontroller *)viewcontroller { bool ended = [self.window makefirstresponder:self.window]; if (!ended) { nsbeep(); return; } [self.box setcontentview:viewcontroller.view]; }
i set target/action nsbutton
appdelegate, , here's call method show new view controller:
- (ibaction)didtapcontinue:(nsbutton *)sender { newviewcontroller *newvc = [[newviewcontroller alloc] init]; [self displayviewcontroller:newvc]; }
this work - presents new view controller's view. if click button in view has target/action set resides within view controller class, app instantly crashes.
to resolve issue, have change didtapcontinue:
following:
- (ibaction)didtapcontinue:(nsbutton *)sender { newviewcontroller *newvc = [[newviewcontroller alloc] init]; [self.viewcontrollers addobject:newvc]; [self displayviewcontroller:[self.viewcontrollers lastobject]]; }
first of all, can explain why resolves issue? seems related way controller "held onto" in memory i'm not positive.
my question is, how set can swap out views within view controller? planning on getting reference appdelegate , calling displayviewcontroller:
new controller instantiated in class, causes crash. need first store in array send reference method. valid approach - make viewcontrollers
array public call method lastobject
, or how should set up?
what interesting in code alloc/init new view controller every time call ibaction. can view totally new every time call ibaction method, think have limited number of views want show. far knowledge goes makes view live long ibaction method long. view still exists, because haven't refreshed it. however, calling method inside view controller not in heap anymore (since left ibaction method , local objects, such view controller taken of heap thans arc) makes app crash, because reference memory space not in use or used else.
why app work when ad view viewcontrollers array? assume array array has been initiated in appdelegate , add view controller strong reference count viewcontrollers array. when leave ibaction method, view controller still has strong reference , arc not deallocate view controller.
is proper way? well, works. not think considered programming, since don't alloc/init object in method needs stay alive after leaving method. better practice allocate , initialize view controller(s) somewhere in init, awakefromnib or windowdidload method of appdelegate. problem current solution creating endless array of view controllers of use last. somewhere program feel burden of enormously long array of pretty heavy objects (view controllers) , run out of memory.
hope helps.
by way, independent of whether use mavericks or yosemite. thinking in storyboard solution, wouldn't answer question.
kind regards, macusert
Comments
Post a Comment