Sunday, March 10, 2013

iOS Development - Navigation, Tab, Scroll

Intro to Navigation Controller
// Create instance of navigation controller
self.navController = [[UINavigationController alloc] init];
    
// Create instance of First View Controller
FirstViewController *firstVC = [[FirstViewController alloc]init];
    
// Push firstVC onto navigation stack
// animated set to NO so that you don't see the page sliding on first load
[self.navController pushViewController:firstVC animated:NO];
    
// Push entire stack onto window (canvas)
[self.window setRootViewController:self.navController];

----------------------------------------

Going to another page from a button in firstVC

- (IBAction)buttonPressed:(id)sender {
    
    // Push in 2nd view controller
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
    
}

----------------------------------------

Intro to Tab Bar Controller

// Initialisation
    self.tabBarController = [[UITabBarController alloc] init];
    RedViewController *redVC = [[RedViewController alloc]init];
    BlueViewController *blueVC = [[BlueViewController alloc]init];

    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

----------------------------------------

Putting a Nav View Controller in a Tab Bar Controller

AppDelegate.m
    self.tabBarController = [[UITabBarController alloc] init];
    self.whiteNavController = [[UINavigationController alloc] init];
    
    RedViewController *redVC = [[RedViewController alloc]init];
    redVC.tabBarItem.title = @"Red";
    redVC.tabBarItem.image = [UIImage imageNamed:@"all.png"];
    
    BlueViewController *blueVC = [[BlueViewController alloc]init];
    blueVC.tabBarItem.title = @"Blue";
    blueVC.tabBarItem.image = [UIImage imageNamed:@"search.png"];
    
    WhiteViewController *whiteVC = [[WhiteViewController alloc]init];
    whiteVC.tabBarItem.title = @"White";
    whiteVC.tabBarItem.image = [UIImage imageNamed:@"faves.png"];
    [self.whiteNavController pushViewController:whiteVC animated:NO];
    
    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, self.whiteNavController, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

-------------------------------------

Scroll View
    // Create UIImageView to store image
    UIImageView *myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature-wallpaper-hd1.jpg"]];
    
    // Set up scroll view content size to the size of the image
    self.myScrollView.contentSize = myImageView.frame.size;
    
    // Add the image view onto scrollview
    [self.myScrollView addSubview:myImageView];


No comments:

Post a Comment