iOS: method to create two views -
i want create 2 side side views add viewcontroller's view. keep repeating code, trying write generic method create 2 views me. however, code isn't working me. both view1 , view2 ivars.
- (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [self makeview:view1]; [self makeview:view2]; } - (void)makeview:(uiview*)view { cgrect frame = self.view.bounds; frame.size.height = frame.size.height/2; if (view == view2) { frame.origin = cgpointmake(0, frame.size.height); } view = [[uiview alloc] initwithframe:frame]; [self.view addsubview:view]; }
i think issue might deal line view == view2, sort of variable reference error. view == view2 evaluates true, view1 never shows up. , in later parts of code view1 , view2 nil.
let's take step step find out answer.
first, viewwillappear
being called, , view1
, view2
both nil
because have not set them yet.
then, calling method on view1
, nil
, parameter going value nil
.
you create frame, , if statement. parameter (view
) nil
, said before, , view2
nil
because, said earlier, started nil
, haven't set yet. because of this, view==view2
true because nil==nil
true, , frame origin wanted view2.
then, setting view
new uiview , adding subview, add view (that wanted view2), still not setting view1
variable.
after this, doing exact same thing view2
, gives view exact same frame because view
, view1
, , view2
still nil
.
in order bypass this, should creating of view1
, view2
outside of method (the view1/2 = [[uiview alloc] init];
) , of setting parts inside of method.
Comments
Post a Comment