holydancer原創,如需轉載,請在顯要位置註明:
轉自holydancer的CSDN專欄,原文地址:http://blog.csdn.net/holydancer/article/details/7484341
Iphone和android手機的一個不同的地方是,大部分的android手機都有返回鍵,而Iphone只有一個home鍵,所以我們會發現在Iphone的大部分應用中會在頂部有一個導航條,比如系統的設置界面,該導航條完全按照棧的方式來管理,所以可以很方便的實現後退的操作:
今天下了春雨,心情不錯,總結一下導航條的使用;
導航欄這個控件稱爲UINavigationController,常常用來作根視圖控制器,生成對象後可以用該對象push UIViewController的對象,這樣該UIViewController的對象就加到導航條的下部了,可以給視圖控制器加title,會顯示在導航欄上,也可以修改返回鍵的title,如右上圖的左上角的setting按鈕,如果默認的話是沒有viewController的title的,返回鍵的title會是viewController的名字,接下來我們要用Xcode4.3中最基礎的模板EmptyApplication來從頭創建一個最原始的導航demo;
首先,新建一個project,選擇EmptyApplication模板;
這樣生成後是隻有委託類的:
然後new兩個ViewController出來,爲了方便操作記得要附上xib文件;起名爲FirstViewController,SecondViewController,在FirstViewController的xib文件中拖上一個button,連接一個nextClick方法用來切換到下一個視圖;
現在開始操作代碼
AppDelegate.h:
- <span style="font-size:18px;">#import <UIKit/UIKit.h>
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (strong, nonatomic) UINavigationController *naviController;
- @end</span>
AppDelegate.m:
- <span style="font-size:18px;">#import "AppDelegate.h"
- #import "FirstViewController.h"
- @implementation AppDelegate
- @synthesize window = _window;
- @synthesize naviController;
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- //生成一個ViewControlle對象作爲導航欄的第一個視圖;
- FirstViewController *firstView = [[FirstViewController alloc]init];
- naviController = [[UINavigationController alloc]initWithRootViewController:firstView];
- //將該導航欄作爲根視圖控制器;
- self.window.rootViewController = naviController ;
- [self.window makeKeyAndVisible];
- return YES;
- }
- </span>
FirstViewController.h:
- <span style="font-size:18px;">#import <UIKit/UIKit.h>
- @interface FirstViewController : UIViewController
- - (IBAction)nextClick:(id)sender;
- @end</span>
FirstViewController.m:
- <span style="font-size:18px;">#import "FirstViewController.h"
- #import "SecondViewController.h"
- @interface FirstViewController ()
- @end
- @implementation FirstViewController
- - (IBAction)nextClick:(id)sender {
- SecondViewController *secondView = [[SecondViewController alloc]init];
- [self.navigationController pushViewController:secondView animated:YES];
- }</span>
上面只是貼出了需要修改的代碼,其餘自動生成的都不用管,甚至SecondViewController都沒有進行操作,所以我們會看到這樣一種最簡單的導航控制效果;
關鍵字:UINavigationController , IOS ,Iphone 開發 ,導航控制器