iOS development--code generation Tabbar and view switching specific explanations

Source: Internet
Author: User

I've explained in a number of previous blogs how to use nib files without using storyboard. Use code to generate navigation bar and jump, specific to the "iOS development-interface jump and return and view type specific explanation" "iOS Pure Code Implementation interface establishment, jump, navigation bar (no storyboard, no nib) (OBJECTIVE-C)."

Today I'm going to explain. In the case of using NIB to build the interface, the code generates Tabbar and jumps between the interfaces. The Code Demo sample has been uploaded to: https://github.com/chenyufeng1991/TabBarTest.

(1) In this demo sample, both navigation and Tabbar are implemented by code. So it is necessary to initialize the settings in appdelegate such as the following: Rootviewcontroller is a root view defined later.

#import "AppDelegate.h" #import "RootViewController.h" @interface appdelegate () @end @implementation appdelegate-(BOOL ) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {  Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];  Declares the root view;  rootviewcontroller *root = [[Rootviewcontroller alloc]init];  Self.window.rootViewController = root;  [Self.window makekeyandvisible];  return YES;} @end


(2) Rootviewcontroller defines the root view, where the navigation and tabbar of the page are defined. This is the first view we have seen.

#import "RootViewController.h" #import "FirstViewController.h" #import "SecondViewController.h" @interface Rootviewcontroller () <uitabbarcontrollerdelegate>//declaration tabbar@property (nonatomic,strong) UITabBarController *  Tabbarcontroller; @end @implementation rootviewcontroller-(void) viewdidload{[Super Viewdidload];  Uitabbarcontroller *tabbarcontroller = [[Uitabbarcontroller alloc]init];  Tabbarcontroller.delegate = self;   /** adds two interfaces to the root view, and two interfaces to the navigation bar respectively.  */Firstviewcontroller *FIRSTVC = [[Firstviewcontroller alloc]init];  Uinavigationcontroller *firstnav = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:FIRSTVC];  Firstnav.tabbaritem = [[Uitabbaritem alloc]initwithtabbarsystemitem:uitabbarsystemitemrecents tag:0];  Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc]init];  Uinavigationcontroller *secondnav = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:SECONDVC]; Secondnav.tabbaritem = [[Uitabbaritem Alloc]initwithtabbarsystemitem:uitabbarsystemiTemsearch Tag:1];  stored through an array.  Tabbarcontroller.viewcontrollers = [Nsarray arraywithobjects:firstnav,secondnav, Nil];  Self.tabbarcontroller = Tabbarcontroller; [Self.view AddSubview:tabBarController.view];} @end

(3) Tabbar's first tab implementation, for example, I'm going to push through a button to have a page (also the navigation bar and Tabbar).

#import "FirstViewController.h" #import "First02ViewController.h" @interface Firstviewcontroller () @ End@implementation firstviewcontroller-(void) viewdidload {  [super viewdidload];  Self.title = @ "1111";} -(Ibaction) buttonpressed: (ID) Sender {  //jump through push to another interface;  first02viewcontroller *first02 = [ First02viewcontroller alloc] init];  [Self.navigationcontroller pushviewcontroller:first02 animated:true];} @end

(4) After the push to an interface, you can return using the "back" button that comes with the navigation bar. can also be returned via pop:

#import "First02ViewController.h" @interface First02viewcontroller () @end @implementation first02viewcontroller-( void) Viewdidload {  [super viewdidload];  Self.title = @ "News";} -(Ibaction) backbuttonpressed: (ID) Sender {  //return to push through the pop interface;  [Self.navigationcontroller Popviewcontrolleranimated:true];} @end


(5) In the Second tab. I modal by clicking button to jump to another page (the page has no navigation bar, no tabbar).

#import "SecondViewController.h" #import "Second02ViewController.h" @interface Secondviewcontroller () @ End@implementation secondviewcontroller-(void) viewdidload {  [super viewdidload];  Self.title = @ "2222";} -(Ibaction) buttonpressed: (ID) Sender {  //jump through modal way, after jumping the interface has no navigation bar.  Second02viewcontroller *second02 = [[Second02viewcontroller alloc] init];  [Self presentviewcontroller:second02 animated:true completion:nil];} @end

Then return via dismiss.

#import "Second02ViewController.h" @interface Second02viewcontroller () @end @implementation second02viewcontroller-( void) Viewdidload {  [Super viewdidload];} -(Ibaction) backbuttonpressed: (ID) Sender {  //via dismiss return to modal interface;  [self dismissviewcontrolleranimated: True Completion:nil];} @end


Look directly at the code above may be a bit messy, you can download the source after the execution of the view.

This can also be used directly as an interface architecture. But suppose you want to use storyboard to develop, it is also very convenient.


GitHub home: https://github.com/chenyufeng1991. Welcome to visit us!

The recent Geek College wiki is in the development of IT career skills Atlas, I am mainly responsible for iOS direction, we are interested in the ability to participate together, there are problems or changes can directly send me issues or pull request. Https://github.com/chenyufeng1991/skillmap.


iOS development--code generation Tabbar and view switching specific explanations

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.