iOS development--code generation Tabbar and view switching

Source: Internet
Author: User

I have explained in a number of previous blog in the case of using the nib file without using storyboard, using code to generate navigation bar and jump, in particular, refer to the iOS development-interface jump and return and the type of view in detail, "iOS Pure Code Implementation interface build, jump, Navigation Bar (no storyboard, no nib) (OBJECTIVE-C). Today I'm going to explain the use of the nib to build the interface, with the code to generate Tabbar, and the interface between the jump. The code sample has been uploaded to: https://github.com/chenyufeng1991/TabBarTest.

(1) In this example, both navigation and Tabbar are implemented by code, so the initialization setting in Appdelegate is as follows: where 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;  /** add 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];  Storage by array; tabbarcontroller.viewcontrollers = [Nsarray arraywithobjects:firstnav,secondnav, Nil];  Self.tabbarcontroller = Tabbarcontroller; [Self.view AddSubview:tabBarController.view];} @end

(3) The first tab of Tabbar is implemented as follows, where I jump to another page with a push button (navigation bar and Tabbar also appear).

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

(4) After the push to another interface, you can return using the "back" button that comes with the navigation bar, or you can return it 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 to another page by clicking the button (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, skip the interface without 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


Looking directly at the code above may be a bit messy, you can view it by downloading the source code after running. This can also be used as an interface to the architecture, but it is also very handy if you want to use storyboard to develop it.


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

The recent Geek College wiki is in the development of IT career skills Atlas, I am mainly responsible for iOS direction, everyone interested can 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

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.