objective c - push and pop information from DetailViewController to MasterViewController -
i'm making small ipad app using splitviewcontroller , working masterviewcontroller on detailviewcontroller. trying push data detailviewcontroller masterviewcontroller. once information pushed masterviewcontroller want make use of im using pop method.
the push method populates array- reason array recreated every time call pushmoduletitle
method , hold 1 object @ time.
in detailviewcontroller.m
- (ibaction)buttonaddpressed:(id)sender { cw3masterviewcontroller *master = [[cw3masterviewcontroller alloc]init]; [moduletitles addobject:textfieldmoduletitle.text]; [master pushmoduletitle:self.textfieldmoduletitle.text];); }
in masterviewcontroller.m
- (nsmutablearray *)moduletitlestack//init array { if (!_moduletitlestack){ _moduletitlestack = [[nsmutablearray alloc] init]; } return _moduletitlestack; } -(void)pushmoduletitle:(nsstring*)moduletitile { nsstring * moduletitileobject = moduletitile; [self.moduletitlestack addobject:moduletitileobject]; nslog(@"%@",self.moduletitlestack); }
so use information pushed i'm using pop method: - alway returns me null value , when put break point indicates moduletitlestack has 0 objects. i'm not sure why.
-(nsstring *)popmoduletitle { nsstring * moduletitileobject = [self.moduletitlestack lastobject]; if (moduletitileobject)[self.moduletitlestack removelastobject]; return moduletitileobject; }
calling popmoduletitle method: gives null value
- (ibaction)testbuttonpressed:(id)sender { nslog(@"%@", [self popmoduletitle]); }
the reason same answer other question asked. creating new instance of cw3masterviewcontroller every time click button. should reference master controller this:
cw3masterviewcontroller *master = self.splitviewcontroller.viewcontrollers[0];
this assumes master controller @ index 0 of split view controller. if it's embedded in navigation controller (which is), need go little further master:
cw3masterviewcontroller *master = (cw3masterviewcontroller *)[(uinavigationcontroller *) self.splitviewcontroller.viewcontrollers[0] topviewcontroller];
Comments
Post a Comment