UINavigationController詳解二頁面切換和SegmentedController

來源:互聯網
上載者:User

標籤:style   blog   http   get   strong   width   

原文出自:http://blog.csdn.net/totogo2010/article/details/7682433,非常感謝。

1、RootView 跳到SecondView

首先我們需要新一個View。建立SecondView,按住Command鍵然後按N,彈出建立頁面,我們建立SecondView

 

2、為Button 添加點擊事件,實現跳轉

在RootViewController.xib中和RootViewController.h檔案建立串連

 

在RootViewController.m中實現代碼,alloc一個SecondViewController,用pushViewController到navigationController中去,並為

 

SecondViewController這是title為    secondView.title =@"Second View"; 預設情況下,titie為下個頁面返回按鈕的名字。

 

[cpp] view plaincopy 
  1. - (IBAction)gotoSecondView:(id)sender {  
  2.     SecondViewController *secondView = [[SecondViewController alloc] init];  
  3.     [self.navigationController pushViewController:secondView animated:YES];  
  4.     secondView.title = @"Second View";  
  5. }  

這是點擊GotoSecondView 按鈕,出現

 

這就是SecondView了。

 

3、添加segmentedController

在nav bar這樣的效果是如何?的呢?

這就是segmentedController。

 

3.1在RootViewController.m的viewDidLoad添加如下代碼:

 

 

[cpp] view plaincopy 
  1. NSArray *array = [NSArray arrayWithObjects:@"雞翅",@"排骨", nil];  
  2.    UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];  
  3.    segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;  
  4.   
  5.    [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  
  6.    self.navigationItem.titleView = segmentedController;  


3.2[segmentedController addTarget:selfaction:的實現

 

 

[cpp] view plaincopy 
  1. -(void)segmentAction:(id)sender  
  2. {  
  3.     switch ([sender selectedSegmentIndex]) {  
  4.         case 0:  
  5.         {  
  6.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了雞翅" delegate:self  cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];  
  7.             [alter show];  
  8.   
  9.         }  
  10.         break;  
  11.     case 1:  
  12.         {  
  13.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了排骨" delegate:self  cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];  
  14.             [alter show];  
  15.         }  
  16.         break;  
  17.           
  18.         default:  
  19.             break;  
  20.     }  
  21. }  

這樣就能響應雞翅和排骨按鈕了

 

4、自訂backBarButtonItem

 

左上方的返回上級View的barButtonitem的名字是上級目錄的Title,如果title或者適合做button的名字,怎麼辦呢?我們可以自己定義

代碼如下:

 

[cpp] view plaincopy 
  1. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根視圖" style:UIBarButtonItemStyleDone target:nil action:nil];  
  2.    self.navigationItem.leftBarButtonItem = backButton;

效果:

 

6、自訂title

UINavigationController的title可以用別view替代,比如用UIButton UILable等,下面我用UIButton.

在SecondViewController.m中添加下面如下。

 

[cpp] view plaincopy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];  
  5.     [button setTitle: @"自訂title" forState: UIControlStateNormal];  
  6.     [button sizeToFit];  
  7.     self.navigationItem.titleView = button;}  

運行程式,goto secondView,運行效果

 

相關文章

聯繫我們

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