Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。

首先需要说明的是每个应用程序都是一个window,背景色为黑色。在window上可以跑多个view进行来回切换,下面就通过手动写代码来体现导航栏切换view的原理。

第一步,新建一个single view工程,然后再新建一个带xib文件的UIviewController。

程序结构如下图:

第二步,在cidpAppDelegate.m文件中修改- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;方法,在函数体中实例化一个UINavigationController对象,在初始化的时候将RootViewController(根视图控制器)初始化为self.viewController,然后将

self.window.rootViewController 初始化为UINavigationController对象 navi;

修改代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[cidpViewController alloc] initWithNibName:@"cidpViewController" bundle:nil]; 
    UINavigationController* navi = [[[UINavigationController alloc] initWithRootViewController:self.viewController]autorelease];
    self.window.rootViewController = navi;
    [self.window makeKeyAndVisible];

    return YES;
}

原函数代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[cidpViewController alloc] initWithNibName:@"cidpViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

第三步:在cidpViewController.h文件中添加方法-(IBAction)goNex;并在cidpViewController.m文件中实现:

实现代码如下:

-(IBAction)goNext{
    //实例化一个试图对象(即将要跳转的试图),
    Second* s = [[Second alloc] initWithNibName:@"Second" bundle:nil];
    //控制试图加载的样式,一些简单的效果
    s.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    //将导航栏添加在跳转后的试图上,
    [self.navigationController pushViewController:s animated:YES];
    
}

第四步:在cidpViewController.m文件中添加导航栏的标题和为导航栏添加BarButtonItem。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"Movie";
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(goNext)]; 
}

这样已经实现了两个视图之间的来回切换,不过为了实现返回button的原理,继续在第二个视图中添加一个button来返回,以彰显原理。

第五步:在第二个视图里面添加返回按钮的代码,在Second.h文件中声明方法- (IBAction)goBack:(id)sender;并在Second.m文件中实现:

实现代码:

- (IBAction)goBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

在Second.m文件中的- (void)viewDidLoad方法中为导航栏添加标题,并且添加返回按钮,是返回按钮使用函数指针的形式调用goBack来返回,

- (void)viewDidLoad
{
    [super viewDidLoad];
//添加标题。 self.navigationItem.title
= @"second";
//初始化返回按钮。 self.navigationItem.rightBarButtonItem
= [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)]; // Do any additional setup after loading the view from its nib. }

这样就实现了导航栏来回切换视图的原理。其实关键的原理就是

[self.navigationController pushViewController:s animated:YES];和
[self.navigationController popViewControllerAnimated:YES];

类似于压栈和出栈,其实就是这样的喊 push和pop嘛。

 

 

posted @ 2013-08-28 20:28  shouqiang Wei  阅读(1762)  评论(0编辑  收藏  举报