In the previous blog post on iOS, we learned how to use the Tab Bar and Switch views. This is to use Tabbar in AppDelegate. To open a program like TabbarView, sometimes we need to provide some help pages for the program, you can also log on to the page and then jump to the tabbar View, or use Tabbar only on the following pages. How can this problem be achieved?
Create a view and use [selfpresentModalViewController: tabBaranimated: YES]; To jump to the view.
When the program needs to switch between multiple views, you can use UINavigationController or ModalViewController. UINabigationController switches multiple views through the navigation bar. If the number of views is relatively small and the display field is full screen, it is more appropriate to use ModalViewController (for example, the view that requires user input information will be automatically returned to the previous view after the end)
1. Create a Single View app and press Command + N to create three new viewcontrollers. The. xib file is selected.
1.1 The newly created controllers are TestOneController TestTwoController TestThirdViewController, both of which inherit from UIViewController.
Click the xib file and modify the View color in the Properties window of the. xib file. This ensures that the page is switched when the page is switched.
Well, my ThirdViewController does not have xib. It may be missing, but it does not matter. It can be used in the same way.
1.2 add TabBarViewController
The most important thing is to add another TabBarViewController, which must inherit the UITabBarController
2. In ViewController, the first page of the program. Add a jump Button, add Action, and jump to tabbar in Action.
Code in ViewController. m. Add all three viewcontrollers created in the previous step to the Tabbar.
-(IBAction) gotoTabbarVIew :( id) sender {NSMutableArray * items = [[NSMutableArray alloc] init]; TestOneController * testOne1 = [[TestOneController alloc] init]; [items addObject: testOne1]; TestTwoController * twoController = [[TestTwoController alloc] init]; [items addObject: twoController]; using * thirdController = [[TestThirdViewController alloc] init]; [items addObject: thirdController]; // items is an array. Each member is a UIViewController TabBarViewController * tabBar = [[TabBarViewController alloc] init]; [tabBar setTitle: @ "TabBarController"]; [tabBar setViewControllers: items]; [self presentModalViewController: tabBar animated: YES];}
In this way, the operation jumps to the TabbarView, but the three tabs in the current tabbarView are blank and black. How to add icons and other styles?
3. Add UITabBarItem
Add the corresponding UITabBarItem to the three ViewController. m files. The Code is as follows:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:1]; self.tabBarItem = item; self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",9]; } return self;}
The Style Constant of UITabBarSystemItemMostRecent can be changed according to your preference. In addition, you can use a custom image as an item. Here, we will not demonstrate how to add images by ourselves.
Self. tabBarItem. badgeValue: Number of the bubble displayed on the tabbar item.
The corresponding ViewController is added, and the tag is written into different numbers, which will be used later. Now it works.
Switch
4. Listen to Item click events
How can I monitor the item you ordered when Tabbar is available?
Implement UITabBarDelegate. I checked in apple's document to implement
-(Void) tabBar :( UITabBar *) tabBar didSelectItem :( UITabBarItem *) the item method can be listened.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{ if(item.tag == 1){ NSLog(@"TestOneController"); }else if(item.tag == 2){ NSLog(@"TestTwoController"); }else { NSLog(@"TestThirdController"); }}
Determine through tag, Run switch: Print log.
2012-06-28 20:56:19.144 View2TaBBarView[5614:f803] TestTwoController2012-06-28 20:56:19.785 View2TaBBarView[5614:f803] TestThirdController2012-06-28 20:56:20.363 View2TaBBarView[5614:f803] TestTwoController2012-06-28 20:56:20.843 View2TaBBarView[5614:f803] TestOneController
5. Return to the previous page
Return to the previous page through [self. parentViewControllerdismissModalViewControllerAnimated: YES ];
Add a button in ThirdView. Add Action events. The Code is as follows:
-(Void) backAction :( id) sender {[self. parentViewController dismissModalViewControllerAnimated: YES];}-(void) viewDidLoad {[super viewDidLoad]; [self. view setBackgroundColor: [UIColor brownColor]; UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (40, 50, 60, 30)]; [button setTitle: @ "return" forState: UIControlStateNormal]; [button addTarget: self action: @ selector (backAction :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button]; // Do any additional setup after loading the view .}
Run. Click "back" to return the first page:
Example code: http://download.csdn.net/detail/totogo2010/4399715
Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!