国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

IOS導(dǎo)航欄的使用方法

2019-11-14 20:05:19
字體:
供稿:網(wǎng)友

本文是使用純代碼實現(xiàn)一個導(dǎo)航欄的效果。單擊按鈕并且產(chǎn)生事件?;舅悸肥牵?/p>

1.創(chuàng)建一個導(dǎo)航欄(UINavigationBar對象)

2.創(chuàng)建一個導(dǎo)航欄集合(UINavigationItem對象)

3.創(chuàng)建一個左邊按鈕、一個右邊按鈕(UIBarButtonItem對象),并實現(xiàn)對應(yīng)的事件方法

4.將導(dǎo)航欄集合添加到導(dǎo)航欄中,設(shè)置動畫關(guān)閉

5.把左右兩個按鈕添加到導(dǎo)航欄集合中去

6.在視圖中顯示當前創(chuàng)建的導(dǎo)航欄

 

具體的實現(xiàn)代碼如下:

ViewController.h文件中的代碼不用改變,如下所示:

 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @end  

 

ViewController.m文件中的代碼:

 

  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     // Do any additional setup after loading the view, typically from a nib.  
  13.       
  14.     //創(chuàng)建一個導(dǎo)航欄  
  15.     UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
  16.     //創(chuàng)建一個導(dǎo)航欄集合  
  17.     UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];  
  18.     //在這個集合Item中添加標題,按鈕  
  19.     //style:設(shè)置按鈕的風格,一共有三種選擇  
  20.     //action:@selector:設(shè)置按鈕的點擊事件  
  21.     //創(chuàng)建一個左邊按鈕  
  22.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左邊" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];  
  23.     //創(chuàng)建一個右邊按鈕  
  24.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右邊" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];  
  25.       
  26.     //設(shè)置導(dǎo)航欄的內(nèi)容  
  27.     [navItem setTitle:@"凌凌漆"];  
  28.       
  29.     //把導(dǎo)航欄集合添加到導(dǎo)航欄中,設(shè)置動畫關(guān)閉  
  30.     [navBar pushNavigationItem:navItem animated:NO];  
  31.       
  32.     //把左右兩個按鈕添加到導(dǎo)航欄集合中去  
  33.     [navItem setLeftBarButtonItem:leftButton];  
  34.     [navItem setRightBarButtonItem:rightButton];  
  35.       
  36.     //將標題欄中的內(nèi)容全部添加到主視圖當中  
  37.     [self.view addSubview:navBar];  
  38.       
  39.     //最后將控件在內(nèi)存中釋放掉,以避免內(nèi)存泄露  
  40.     [navItem release];  
  41.     [leftButton release];  
  42.     [rightButton release];  
  43. }  
  44.   
  45. -(void)showDialog:(NSString *)str  
  46. {  
  47.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"這是一個對話框" message:str delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];  
  48.     [alert show];  
  49.     [alert release];  
  50. }  
  51.   
  52. -(void) clickLeftButton  
  53. {  
  54.     [self showDialog:@"點擊了導(dǎo)航欄左邊按鈕"];  
  55. }  
  56.   
  57. -(void) clickRightButton  
  58. {  
  59.     [self showDialog:@"點擊了導(dǎo)航欄右邊按鈕"];  
  60. }  
  61.   
  62. - (void)viewDidUnload  
  63. {  
  64.     [super viewDidUnload];  
  65.     // Release any retained subviews of the main view.  
  66. }  
  67.   
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  69. {  
  70.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  71. }  
  72.   
  73. @end  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉川市| 靖州| 东港市| 新民市| 塔河县| 阳朔县| 清新县| 武宁县| 乾安县| 涞源县| 宜都市| 房山区| 鲁甸县| 齐河县| 荥阳市| 潼关县| 锡林郭勒盟| 泰兴市| 江川县| 伊通| 道孚县| 砚山县| 鲁山县| 克山县| 高淳县| 综艺| 色达县| 阜新市| 衡南县| 太仓市| 宕昌县| 新泰市| 南木林县| 荆州市| 留坝县| 涿州市| 建湖县| 囊谦县| 呈贡县| 噶尔县| 芦溪县|