iOS學習之UINavigationController詳解與使用(一)添加UIBarButtonItem

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/totogo2010/article/details/7681879

1、UINavigationController導航控制器如何使用

UINavigationController可以翻譯為導航控制器,在iOS裡經常用到。

我們看看它的如何使用:

下面的圖顯示了導航控制器的流程。最左側是根視圖,當使用者點擊其中的General項時 ,General視圖會滑入螢幕;當使用者繼續點擊Auto-Lock項時,Auto-Lock視圖將滑入螢幕。相應地,在對象管理上,導航控制器使用了導航堆棧。根視圖控制器在堆棧最底層,接下來入棧的是General視圖控制器和Auto-Lock視圖控制器。可以調用pushViewControllerAnimated:方法將視圖控制器推入棧頂,也可以調用popViewControllerAnimated:方法將視圖控制器彈出堆棧。

來自蘋果官網。

 

2、UINavigationController的結構組成

看,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等組成。

 

現在我們建立一個例子,看看如何使用UINavigationController

3、建立一個項目

命名為UINavigationControllerDemo,為了更好理解UINavigationController,我們選擇Empty Application模板

4、建立一個View Controller,命名為RootViewController:依次選擇File——New——New File,預設勾上With XIB for user interface.

選擇正確位置建立完成,這時項目裡多了三個檔案,分別是RootViewController.h RootViewController.m RootViewController.xib檔案。

開啟RootViewController.xib,添加一個按鈕控制項,按鈕Button改成 :Goto SecondView,為跳轉做準備

 

5、開啟AppDelegate.h,向其中添加屬性:

 

[cpp] view plain copy 
  1. @property (strong, nonatomic) UINavigationController *navController;  


添加後AppDelegate.h檔案代碼如下:

 

 

[cpp] view plain copy 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class ViewController;  
  4.   
  5. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  6.   
  7. @property (strong, nonatomic) UIWindow *window;  
  8.   
  9. @property (strong, nonatomic) ViewController *viewController;  
  10.   
  11. @property (strong, nonatomic) UINavigationController *navController;  
  12.   
  13. @end  


6、在AppDelegate.m 檔案的didFinishLaunchingWithOptions方法中建立添加navController,RootViewController視圖。

 

 

[cpp] view plain copy 
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     RootViewController *rootView = [[RootViewController alloc] init];  
  5.     rootView.title = @"Root View";  
  6.       
  7.     self.navController = [[UINavigationController alloc] init];  
  8.     [self.navController pushViewController:rootView animated:YES];  
  9.     [self.window addSubview:self.navController.view];  
  10.     [self.window makeKeyAndVisible];  
  11.     return YES;  
  12. }  

給rootView的titie命名為 Root View,好識別View直接的切換關係。用pushViewController把rootView加入到navController的視圖棧中。

 

7、現在Root視圖添加完成

看看效果:

現在還沒有Navigation bar 。只有title。

8、添加UIBarButtonItem

bar ButtonItem分左右UIBarButtonItem。我們把左右的都添加上去。

在RootViewController.m中添加代碼如下:

 

[cpp] view plain copy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];  
  6.     self.navigationItem.leftBarButtonItem = leftButton;  
  7.       
  8.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];  
  9.     self.navigationItem.rightBarButtonItem = rightButton;<p class="p1">}</p>  

這樣添加了UIBarButtonItem了,效果如下:

 

這裡重點介紹下

UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

UIBarButtonSystemItemAction的風格,這是系統內建的按鈕風格,看,你不用一個個實驗,你也知道想用那個item,如:


9、響應UIBarButtonItem的事件的實現

我們在 action:@selector(selectLeftAction:);

action添加了selectLeftAction和selectRightAction

在RootViewController.m檔案中添加代碼實現:

 

[cpp] view plain copy 
  1. -(void)selectLeftAction:(id)sender  
  2. {  
  3.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了導覽列左按鈕" delegate:self  cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];  
  4.     [alter show];  
  5. }  
  6.   
  7. -(void)selectRightAction:(id)sender  
  8. {  
  9.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了導覽列右按鈕" delegate:self  cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];  
  10.     [alter show];  
  11. }  

這樣在點擊左右的UIBarButtonItem時,彈出提示:

 



這篇先講添加UIBarButtonItem,下篇講解頁面跳轉和添加UISegmentedControl

 

下篇:iOS學習之UINavigationController詳解與使用(二)頁面切換和segmentedController

 

iOS學習之UINavigationController詳解與使用(一)添加UIBarButtonItem

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.