objective c - Unable to show child view (iOS view container) -
in ios application, have main view controller 3 buttons, work tab bar: when click 1 of buttons, new view controller invoked. tried implement via container view containers, tried following guide (http://www.thinkandbuild.it/working-with-custom-container-view-controllers/) , invoke presentdetailcontroller method in viewdidload of main controller.
actually, no views showed: can me figuring out why? thanks.
viewcontroller.h
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uibutton *btnone; @property (weak, nonatomic) iboutlet uibutton *btntwo; @property (weak, nonatomic) iboutlet uibutton *btnthree; @property (weak, nonatomic) iboutlet uiview *detailview; - (ibaction)click:(id)sender; @end
viewcontroller.m
#import "viewcontroller.h" #import "firstviewcontroller.h" @interface viewcontroller () @property uiviewcontroller *currentdetailviewcontroller; @end @implementation viewcontroller @synthesize btnone, btntwo, btnthree; - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. firstviewcontroller *fvc = [[firstviewcontroller alloc]initwithstring:@"i'm first controller!"]; [self presentdetailcontroller:fvc]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)click:(id)sender { // button selection stuff [self adddetailcontroller:sender]; } - (void)presentdetailcontroller:(uiviewcontroller*)detailvc{ //0. remove current detail view controller showed if(self.currentdetailviewcontroller){ [self removecurrentdetailviewcontroller]; } //1. add detail controller child of container [self addchildviewcontroller:detailvc]; //2. define detail controller's view size detailvc.view.frame = [self framefordetailcontroller]; //3. add detail controller's view container's detail view , save reference detail view controller [self.detailview addsubview:detailvc.view]; self.currentdetailviewcontroller = detailvc; //4. complete add flow calling function didmovetoparentviewcontroller [detailvc didmovetoparentviewcontroller:self]; } - (void)removecurrentdetailviewcontroller{ //1. call willmovetoparentviewcontroller nil // last method detailviewcontroller can perform operations before neing removed [self.currentdetailviewcontroller willmovetoparentviewcontroller:nil]; //2. remove detailviewcontroller's view container [self.currentdetailviewcontroller.view removefromsuperview]; //3. update hierarchy" // automatically method didmovetoparentviewcontroller: called on detailviewcontroller) [self.currentdetailviewcontroller removefromparentviewcontroller]; } - (cgrect)framefordetailcontroller{ // newframe's height should currentframe's height minus buttons' height cgrect detailframe = cgrectmake(0, 0, self.detailview.bounds.size.width, self.detailview.bounds.size.height-self.btnone.frame.size.height); return detailframe; } - (void)adddetailcontroller:(id)sender { firstviewcontroller *detailvc = [[firstviewcontroller alloc]initwithstring:@"first button clicked"]; [self presentdetailcontroller:detailvc]; } @end
firstviewcontroller.h
#import <uikit/uikit.h> @class viewcontroller; @interface firstviewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uilabel *label; @property (weak, nonatomic) iboutlet uibutton *btnone; @property (weak, nonatomic) iboutlet uibutton *btntwo; @property (weak, nonatomic) iboutlet uibutton *btnthree; - (id)initwithstring:(nsstring*)string; @end
firstviewcontroller.m
#import "firstviewcontroller.h" @interface firstviewcontroller (){ nsstring *text; } @end @implementation firstviewcontroller - (id)initwithstring:(nsstring*)string { self = [super init]; if(self){ text = string; } return self; } - (void)viewdidload { [super viewdidload]; self.label.text = text; } @end
as part of creating firstviewcontroller
, calling [super init]
produces bare uiviewcontroller
. diagram, seems if want load firstviewcontroller
storyboard instead.
i suggest sequence create controller using instantiateviewcontrollerwithidentifier:
, set string property in controller want use label, , assign string label when controller's view loads.
Comments
Post a Comment