iOS開發——代碼產生TabBar與視圖切換具體解釋

來源:互聯網
上載者:User

標籤:font   pull   str   dict   ack   ica   margin   ida   數組   

        我在之前多篇部落格中解說了在不使用storyboard而使用nib檔案的情況下。使用代碼產生導覽列並進行跳轉,具體能夠參考《iOS開發——介面跳轉與返回及檢視類型具體解釋》《iOS純程式碼實現介面建立、跳轉、導覽列(無storyboard、無nib)(Objective-C)》。

今天我來解說下在使用nib搭建介面的情況下,用代碼產生TabBar,並進行介面之間的跳轉。代碼示範範例已經上傳至:https://github.com/chenyufeng1991/TabBarTest   。

(1)在該示範範例中,Navigation和TabBar都會通過代碼來實現。所以須要在AppDelegate中初始化設定例如以下:當中RootViewController是在後面定義的一個根視圖。

#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]];  //聲明根視圖;  RootViewController *root = [[RootViewController alloc]init];  self.window.rootViewController = root;  [self.window makeKeyAndVisible];  return YES;}@end


(2)RootViewController定義了根視圖,在這裡定義了頁面的Navigation和TabBar。這是我們第一個看到的視圖。

#import "RootViewController.h"#import "FirstViewController.h"#import "SecondViewController.h"@interface RootViewController ()<UITabBarControllerDelegate>//聲明TabBar@property (nonatomic,strong)UITabBarController *tabBarController;@end@implementation RootViewController- (void)viewDidLoad{  [super viewDidLoad];  UITabBarController *tabBarController = [[UITabBarController alloc]init];  tabBarController.delegate = self;  /**   把兩個介面增加到根視圖中;   兩個介面也分別要導覽列。   */  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];  //通過數組儲存。  tabBarController.viewControllers = [NSArray arrayWithObjects:firstNav,secondNav, nil];  self.tabBarController = tabBarController;  [self.view addSubview:tabBarController.view];}@end

(3)TabBar的第一個Tab實現例如以下,我這裡通過一個button以push方式跳到還有一個頁面(也會出現導覽列和TabBar)。

#import "FirstViewController.h"#import "First02ViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad {  [super viewDidLoad];  self.title = @"1111";}- (IBAction)buttonPressed:(id)sender {  //通過push跳到還有一個介面;  First02ViewController *first02 = [[First02ViewController alloc] init];  [self.navigationController pushViewController:first02 animated:true];}@end

(4)在上述push到還有一個介面後,能夠使用導覽列內建的“返回”button返回。也能夠通過pop返回:

#import "First02ViewController.h"@interface First02ViewController ()@end@implementation First02ViewController- (void)viewDidLoad {  [super viewDidLoad];  self.title = @"新聞";}- (IBAction)backButtonPressed:(id)sender {  //通過pop返回到push過來的介面;  [self.navigationController popViewControllerAnimated:true];}@end


(5)在第二個Tab中。我通過點擊button以Modal方式跳轉到還有一個頁面(該頁面沒有導覽列,沒有TabBar)。

#import "SecondViewController.h"#import "Second02ViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {  [super viewDidLoad];  self.title = @"2222";}- (IBAction)buttonPressed:(id)sender {  //通過modal方式跳轉,跳過去後的介面沒有導覽列。  Second02ViewController *second02 = [[Second02ViewController alloc] init];  [self presentViewController:second02 animated:true completion:nil];}@end

然後通過dismiss返回。

#import "Second02ViewController.h"@interface Second02ViewController ()@end@implementation Second02ViewController- (void)viewDidLoad {  [super viewDidLoad];}- (IBAction)backButtonPressed:(id)sender {  //通過dismiss返回modal過來的介面;  [self dismissViewControllerAnimated:true completion:nil];}@end


      直接看上面的代碼可能有點亂,你能夠通過下載源碼執行後查看。

這個也能夠作為介面的架構直接使用。可是假設你想用storyboard來開發,也是極為方便的。


github首頁:https://github.com/chenyufeng1991  。歡迎大家訪問!

近期極客學院Wiki進行中IT職業技能圖譜的制定,我主要負責iOS方向,大家感興趣的能夠一起參加,有問題或者改動能夠直接給我發issues或者pull request。https://github.com/chenyufeng1991/skillmap  。


iOS開發——代碼產生TabBar與視圖切換具體解釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.