110 likes | 269 Views
Exploring the GUI. What if we want to add a UIView (for example a UILabel) at run time inside the navigation bar? We need to access that view, create a label, and add it as a subview How do we find that view?. Exploring the GUI. From a UIView named myView, we can go up
E N D
Exploring the GUI • What if we want to add a UIView (for example a UILabel) at run time inside the navigation bar? • We need to access that view, create a label, and add it as a subview • How do we find that view?
Exploring the GUI • From a UIView named myView, we can go up • myView.superview (a UIView) • or down • myView.subviews (a NSArray of UIViews)
Exploring the GUI • From a UIViewController named myVC, we can go up • myVC.parentViewController (a UIViewController) • or down • myVC.childViewControllers (a NSArray of UIViewControllers)
Exploring the GUI • Inside the DetailViewController, get the parent view controller, look at its view, and the children of its view • UIViewController *parentC = self.parentViewController; • UIView *topView = parentC.view; • NSArray *views = topView.subviews;
Exploring the GUI • Use NSLog to output the views: there are 2 of them • iPad dimensions are: • UINavigationTransitionView: 0, 0, 703, 768 • UINavigationBar: 0, 20, 703, 44 • navigation bar is on top and its height is 44 pixels
Exploring the GUI • Get the navigation bar view • UINavigationBar *bar = (UINavigationBar *) [views objectAtIndex: 1]; • Now place a label inside it
Exploring the GUI • Run the app, back and forth • The label stays when we go back • Inside the viewWillAppear method of MasterViewController, we can remove it
Exploring the GUI • If we have a method named removeLabel in the DetailViewController class, we could call it from viewWillAppear inside the MasterViewController class • [self.detailViewController removeLabel];
Exploring the GUI • Inside removeLabel, we could search for the label, based on some characteristics (class name, color, text, ..) • Better, we can tag it when we add it • UIView has a property named tag (NSInteger) • label.tag = 99;
Exploring the GUI • From a parent view, we can use the method viewWithTag (searches current view and subviews) to retrieve the label • UILabel *label = [someParentView viewWithTag: 99]; • Now remove it • [label removeFromSuperview];
Exploring the GUI • We could also declare the UILabel as an instance variable, create it when the view loads, and then set it visible or invisible using the hidden property • label.hidden = YES; • label.hidden = NO;