IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
轉: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 plaincopy添加后AppDelegate.h文件代碼如下: [cpp] view plaincopy
6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中創建添加navController,RootViewController視圖。 [cpp] view plaincopy
7、現在Root視圖添加完成
看看效果:
'
現在還沒有Navigation bar 。只有title。
8、添加UIBarButtonItem
bar ButtonItem分左右UIBarButtonItem。我們把左右的都添加上去。
在RootViewController.m中添加代碼如下:
這里重點介紹下
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];
UIBarButtonSystemItemAction的風格,這是系統自帶的按鈕風格,看下圖,你不用一個個試驗,你也知道想用那個item,如下圖:
9、響應UIBarButtonItem的事件的實現
我們在?action:@selector(selectLeftAction:);
action添加了selectLeftAction和selectRightAction
在RootViewController.m文件中添加代碼實現:
[cpp] view plaincopy
這篇先講添加UIBarButtonItem,下篇講解頁面跳轉和添加UISegmentedControl
下篇:IOS學習之UINavigationController詳解與使用(二)頁面切換和segmentedController
例子代碼:https://github.com/schelling/YcDemo
著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝轉載于:https://www.cnblogs.com/jackljf/archive/2012/09/16/3589286.html
總結
以上是生活随笔為你收集整理的IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sqlserver 2008 多表更新
- 下一篇: android call requir