Home > Articles > Home & Office Computing > Entertainment/Gaming/Gadgets

  • Print
  • + Share This
This chapter is from the book

Recipe: Navigating Between View Controllers

In addition to providing menus, navigation controllers do the job they were designed to do: managing hierarchy as you navigate between views. Recipe 5-3 introduces the navigation controller as an actual navigation controller, pushing views on the stack.

The views in this recipe present a number, indicating how many view controllers have been pushed onto the stack. An instance variable stores the current depth number, which is used to both show the current level and decide whether to display a further push option. The maximum depth in this example is 6. In real use, you’d use more meaningful view controllers or contents. This sample demonstrates things at their simplest level.

The navigation controller automatically creates the Level 2 Back button shown in Figure 5-3 (left) as an effect of pushing the new Level 3 controller onto the stack. The rightmost button (Push) triggers navigation to the next controller by calling pushViewController:animated:. When pushed, the next Back button reads Level 3, as shown in Figure 5-3 (right).

Figure 3 (Click to Enlarge) The navigation controller automatically creates properly labeled Back buttons. After selecting the Push 4 button in the left interface, the navigation controller pushes the Level 4 view controller and creates the Level 3 Back button in the right interface.

Back buttons pop the controller stack for you, releasing the current view controller as you move back to the previous one. Make sure your memory management allows that view controller to return all its memory upon being released. Beyond basic memory management, you do not need to program any popping behavior yourself. Note that Back buttons are automatically created for pushed view controllers but not for the root controller itself, as it is not applicable.

Recipe 5-3  Drilling Through Views with UINavigationController

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

@interface NumberViewController : UIViewController
@property (nonatomic, assign) int number;
@property (nonatomic, readonly, assign) UITextView *textView;
+ (id) controllerWithNumber: (int) number;
@end

@implementation NumberViewController
@synthesize number;

// Return a new view controller at the specified level number
+ (id) controllerWithNumber: (int) number
{
    NumberViewController *viewController = 
        [[NumberViewController alloc] init];
    viewController.number = number;
    viewController.textView.text = 
        [NSString stringWithFormat:@"Level %d", number];
    return viewController;
}


// Increment and push a controller onto the stack
- (void) pushController: (id) sender
{
    NumberViewController *nvc = 
        [NumberViewController controllerWithNumber:number + 1];
    [self.navigationController pushViewController:nvc animated:YES];
}


// Set up the text and title
- (void) viewDidAppear: (BOOL) animated
{  
    // match the title to the text view
    self.title = self.textView.text; 
    self.textView.frame = self.view.frame;
    
    // Add a right bar button that pushes a new view
    if (number < 6)
        self.navigationItem.rightBarButtonItem = 
        BARBUTTON(@"Push", @selector(pushController:));
}

// Create the view
- (void) loadView
{
    [super loadView];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
    textView.frame = [[UIScreen mainScreen] bounds];
    textView.font = 
        [UIFont fontWithName:@"Futura" size:IS_IPAD ? 192.0f : 96.0f];
    textView.textAlignment = UITextAlignmentCenter;
    textView.editable = NO;
    textView.tag = 1776;
    textView.autoresizingMask = self.view.autoresizingMask;
    [self.view addSubview:textView];
}

// Access the text view from outside the object
- (UITextView *) textView
{
    return (UITextView *)[self.view viewWithTag:1776];
}
@end 
  • + Share This
  • 🔖 Save To Your Account