資源描述:
《ios開(kāi)發(fā)之導(dǎo)航欄實(shí)現(xiàn)方法》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、IT在線教育平臺(tái)———麥子學(xué)院:http://www.maiziedu.com我們下例IOS導(dǎo)航欄實(shí)現(xiàn)方法是通過(guò)單擊按鈕產(chǎn)生事件,基本思路如下:1.創(chuàng)建一個(gè)導(dǎo)航欄(UINavigationBar對(duì)象)2.創(chuàng)建一個(gè)導(dǎo)航欄集合(UINavigationItem對(duì)象)3.創(chuàng)建一個(gè)左邊按鈕、一個(gè)右邊按鈕(UIBarButtonItem對(duì)象),并實(shí)現(xiàn)對(duì)應(yīng)的事件方法4.將導(dǎo)航欄集合添加到導(dǎo)航欄中,設(shè)置動(dòng)畫(huà)關(guān)閉5.把左右兩個(gè)按鈕添加到導(dǎo)航欄集合中去6.在視圖中顯示當(dāng)前創(chuàng)建的導(dǎo)航欄??具體的實(shí)現(xiàn)代碼如下:ViewCon
2、troller.h文件中的代碼不用改變,如下所示:1.#import??2.??3.@interfaceViewController:UIViewController??IT在線教育平臺(tái)———麥子學(xué)院:http://www.maiziedu.com1.??2.@endViewController.m文件中的代碼:1.#import"ViewController.h"??2.??3.@interfaceViewController()??4.??5.@end??6.??7.@
3、implementationViewController??8.??9.-(void)viewDidLoad??10.{??11.??[superviewDidLoad];??12.??//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.??13.????14.??//創(chuàng)建一個(gè)導(dǎo)航欄??15.??UINavigationBar*navBar=[[UINavigationBaralloc]initWithFrame:CGRectMake(
4、0,0,320,44)];??16.??//創(chuàng)建一個(gè)導(dǎo)航欄集合??17.??UINavigationItem*navItem=[[UINavigationItemalloc]initWithTitle:nil];??18.??//在這個(gè)集合Item中添加標(biāo)題,按鈕??19.??//style:設(shè)置按鈕的風(fēng)格,一共有三種選擇??20.??//action:@selector:設(shè)置按鈕的點(diǎn)擊事件??21.??//創(chuàng)建一個(gè)左邊按鈕??IT在線教育平臺(tái)———麥子學(xué)院:http://www.maiziedu.co
5、m1.??UIBarButtonItem*leftButton=[[UIBarButtonItemalloc]initWithTitle:@"左邊"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(clickLeftButton)];??2.??//創(chuàng)建一個(gè)右邊按鈕??3.??UIBarButtonItem*rightButton=[[UIBarButtonItemalloc]initWithTitle:@"右邊"style:UI
6、BarButtonItemStyleDonetarget:selfaction:@selector(clickRightButton)];??4.????5.??//設(shè)置導(dǎo)航欄的內(nèi)容??6.??[navItemsetTitle:@"凌凌漆"];??7.????8.??//把導(dǎo)航欄集合添加到導(dǎo)航欄中,設(shè)置動(dòng)畫(huà)關(guān)閉??9.??[navBarpushNavigationItem:navItemanimated:NO];??10.????11.??//把左右兩個(gè)按鈕添加到導(dǎo)航欄集合中去??12.??[navIt
7、emsetLeftBarButtonItem:leftButton];??13.??[navItemsetRightBarButtonItem:rightButton];??14.????15.??//將標(biāo)題欄中的內(nèi)容全部添加到主視圖當(dāng)中??16.??[self.viewaddSubview:navBar];??17.????18.??//最后將控件在內(nèi)存中釋放掉,以避免內(nèi)存泄露??19.??[navItemrelease];??20.??[leftButtonrelease];??21.??[righ
8、tButtonrelease];??22.}??23.??24.-(void)showDialog:(NSString*)str??25.{??26.??UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"這是一個(gè)對(duì)話框"message:strdelegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil];??IT在線教育平臺(tái)———麥