您的位置:首页 > 产品设计 > UI/UE

Iphone开发用导航控制器实现视图的分层切换(UINavigationController)

2013-12-07 19:42 447 查看
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:

[plain]
view plaincopyprint?

<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>

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic)  UINavigationController *naviController;
@end


AppDelegate.m:

[plain]
view plaincopyprint?

<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>

#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;
}


FirstViewController.h:

[plain]
view plaincopyprint?

<SPAN style="FONT-SIZE: 18px">#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController - (IBAction)nextClick:(id)sender; @end</SPAN>

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
- (IBAction)nextClick:(id)sender;

@end


FirstViewController.m:

[plain]
view plaincopyprint?

<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>

#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];
}


上面只是贴出了需要修改的代码,其余自动生成的都不用管,甚至SecondViewController都没有进行操作,所以我们会看到这样一种最简单的导航控制效果;





关键字:UINavigationController , IOS ,Iphone 开发 ,导航控制器
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: