Saturday, January 07, 2012

iOS: UINavigationController


// in app delegate .m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    
    //NEW - start
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = nc;
    [nc release];
    // end

    [self.window makeKeyAndVisible];
    return YES;
}

// in viewcontroller .m ; assume IBAction to a button to click to go to another VC
-(IBAction) m1:(id)sender
{
    NSLog(@"about us");
    // NEW: create VC instance and push into nav controller
    AboutVC *about = [[AboutVC alloc] initWithNibName:@"AboutVC" bundle:nil];
    [self.navigationController pushViewController:about animated:YES];
}

// in AboutVC.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // NEW: set title on title bar of nav controller
    [self setTitle:@"About Us"];
}

No comments: