The use of three-level controllers for iOS development and the Custom tab toolbar

Source: Internet
Author: User

Three-level controller concept: uitabbarcontroller-(Management) Uinavigationcontroller---(management) Uiviewcontroller


The following author will share the use of three-level controller

#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 three controller

[Self _creatview];


Customizing the Label toolbar

[Self _newinittabbar];

    

}

Create a Level three controller

-(void) _creatview{

1. Create the 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 the navigation controller and hand over the view controller to the navigation controller and add the navigation controller to an array


Nsmutablearray *navctrls = [[Nsmutablearray alloc] init];

for (int i=0; i<5; i++) {

get the View controller

Uiviewcontroller *viewctrl = viewctrls[i];

creating 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 label controller and give the navigation controller to the tag controller management

      

Self.viewcontrollers = Navctrls;


}

Customizing the Label toolbar

-(void) _newinittabbar {


(1) remove the buttons on the toolbar

get all the child views on the Tabbar

Nsarray *views = [Self.tabbar subviews];

For (UIView *view in views) {

[View Removefromsuperview];

}

(2) setting the background

Self.tabBar.backgroundImage = [UIImage imagenamed:@ "NAVBG"];

(3) Create button

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

// 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 picture

_selectedimg = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ " check "];

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

[Self.tabbar addsubview:_selectedimg];

}


// button click event

-(void) Buttonaction: (UIButton *) button {


// Toggle View Controller

Self.selectedindex = Button.tag;

Animation

[UIView Beginanimations:nil Context:nil];

[UIView setanimationduration:.2];

_selectedimg.center = Button.center;

[UIView commitanimations];

}





Use of the three-level controller for iOS development and the Custom label toolbar

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.