Use of a third-level controller in IOS development and custom label Toolbar

Source: Internet
Author: User

Use of a third-level controller in IOS development and custom label Toolbar

Level 3 controller concept: UITabBarController-> (Management) UINavigationController --> (Management) UIViewController

I will share with you the usage of Level 3 controllers.

# Import "MainTabbarController. h"

 

# Import "ProfileViewController. h"

# Import "GroupViewController. h"

# Import "SearchViewController. h"

# Import "CommentViewController. h"

# Import "MessageViewController. h"

 

@ Interface UITabBarController ()

 

@ End

 

@ Implementation UITabBarController

 

 

-(Void) viewDidLoad {

 

// Create a level-3 Controller

[Self _ creatView];

 

// Customize the label Toolbar

[Self _ newInitTabbar];

 

 

}

// Create a level-3 Controller

-(Void) _ creatView {

 

 

// 1. Create a View Controller and add the View Controller to the array.

ProfileViewController * profileCtrl = [[[ProfileViewController alloc] init] autorelease];

 

 

GroupViewController * groupCtrl = [[[GroupViewController alloc] init] autorelease];

SearchViewController * searchCtrl = [[[SearchViewController alloc] init] autorelease];

CommentViewController * commentCtrl = [[[CommentViewController alloc] init] autorelease];

MessageViewController * messageCtrl = [[MessageViewController alloc] init] autorelease];

// Store the View Controller in an array

NSArray * viewCtrls = @ [profileCtrl, groupCtrl, searchCtrl, commentCtrl, messageCtrl];

 

// 2. Create a navigation controller, assign the View Controller to the navigation controller for management, and add the navigation controller to an array.


NSMutableArray * navCtrls = [[NSMutableArray alloc] init];

For (int I = 0; I <5; I ++ ){

 

// Obtain the View Controller

UIViewController * viewCtrl = viewCtrls [I];

 

// Create a navigation Controller

UINavigationController * navCtrl = [[UINavigationController alloc] initWithRootViewController: viewCtrl];

[NavCtrl. navigationBar setBackgroundImage: [UIImage imageNamed: @ "navbar_bg_normal"] forBarMetrics: UIBarMetricsDefault];

[NavCtrls addObject: navCtrl];

 

}

 

// 3. Create a tag controller and hand it over to the tag controller for management

Self. viewControllers = navCtrls;


}

// Customize the label Toolbar

 

-(Void) _ newInitTabbar {

 

// (1) Remove the button from the toolbar

// Retrieve all subviews on the tabbar

NSArray * views = [self. tabBar subviews];

For (UIView * view in views ){

[View removeFromSuperview];

}

 

// (2) set the background

Self. tabBar. backgroundImage = [UIImage imageNamed: @ "navbg"];

 

// (3) create button

CGFloat width = [UIScreen mainScreen]. bounds. size. width;

// The width of each button

CGFloat w = width/5;

For (int I = 0; I <5; I ++ ){

UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];

NSString * imageName = [NSString stringWithFormat: @ "% d", I + 1];

[Button setImage: [UIImage imageNamed: imageName] forState: UIControlStateNormal];

// Set frame

Button. frame = CGRectMake (w-42)/2 + w * I, 2, 42, 45 );

// Add a click event

[Button addTarget: self action: @ selector (buttonAction :) forControlEvents: UIControlEventTouchUpInside];

[Self. tabBar addSubview: button];

}

 

// (4) create a selected Image

_ SelectedImg = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "selected"];

_ SelectedImg. frame = CGRectMake (w-53)/2.0, 2, 53, 45 );

[Self. tabBar addSubview: _ selectedImg];

 

}

 

// Button click event

-(Void) buttonAction :( UIButton *) button {

 

// Switch the View Controller

Self. selectedIndex = button. tag;

 

// Animation

[UIView beginAnimations: nil context: nil];

[UIView setAnimationDuration:. 2];

 

_ SelectedImg. center = button. center;

 

[UIView commitAnimations];

 

}



 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.