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:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *naviController;
@end
#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;
}
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
- (IBAction)nextClick:(id)sender;
@end
#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];
}
关键字:UINavigationController , IOS ,Iphone 开发 ,导航控制器