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