Implementing Navigation with UINavigationControlle

问题:

     你想要允许你的用户在视图之间来回切换

解决方法:

     

     使用UINavigationController

讨论:

     选择Empty Application取什么名字你随意,然后选择菜单文件选择New->New File...选择第一个objective-c Class这个文件,命名为FirstViewController,继承自UINavigationController。

     基本文件搞定,然后我们开始,找到APP的代理文件 AppDelegate.m文件,然后写下如下代码:

                 #import “AppDelegate.h”

                 #import “FirstViewController.h”

                 @interface AppDelegate ()

                  @property(nonatiomic,strong) UINavigationController *navigationController;

                 @end

               @implementation AppDelegate

             …

    现在我们要使用 initWithRootViewController : 方法初始化导航栏,并且传递我们的参数。然后导航栏就作为根视图出现在窗口上。这里千万不要混淆,UINavigationController是UIViewController的子类,这里的RootViewController可以是任何对象,所以我们希望我们的根视图控制器是一个导航控制器,就只需设置导航控制器为根视图控制器。     那么开始动手写代码

      - (BOOL)  application:(UIApplication *)application didFinishLauchingWithOptions: (NSDictionary *) lauchOptions

  

{

   FirstViewController *viewController = [[FirstViewController alloc ] initWithName:nil bundle:nil];

   self.navigationController = [[UINavigationController alloc ] initWithRootViewController:viewController];

   self.window = [[UIWindow alloc] initWithName: [UIScreen mainScreen] bounds];

   

   self.window.rootViewController = self.navigationController;

   self.window.backgroundColor = [UIColor whiteColor];

  [self.window makeKeyAndVisible];

  

   return YES;

}

  现在在模拟器中运行这个应用看: 应该出现这个图片:

Implementing Navigation with UINavigationControlle

图:1-33 显示导航栏控制器

可能会注意到导航栏显示“First Controller”,这难道是新的部件?并不是,这也是导航条,我们会在很多的导航中使用到那个bar,这里的bar显示的是一个文本,每个视图控制器指定了一个标题本身,一旦视图控制器被压入堆栈,导航控制器就会自动显示这个文本!

  找到我们的根视图控制器的实现稳健,在ViewDidLoad方法里面,设置标题并且添加按钮,当用用户按下这个按钮,就会跳到第二个视图控制器! (还是在AppDelegate.m文件里,然后代码如下:)

      #import “FirstController.h”

      #import “SecondController.h”

     @interface FirstViewController ()

            @property (nonatomic, strong) UIButton *displaySecondViewController;

     @end

    

    @implemetation FirstViewController

 

    - (void) performDisplaySecondViewController: (id)parmamSender

{

     SecondViewController *secondController  = [[SecondViewController alloc] initWithName:nil  bundle:YES];

      [self.navigationContoller pushViewContoller : secondController  animated: YES];

}

   - (void)viewDidLoad

{

   [super viewDidLoad];

   self.title = @“First Controller”;

   

   self.displaySecondViewController  = [UIButton buttonWithType:UIButtonTypeSystem];

  [self.displaySecondViewController setTitle:@“Display Second View Controller”  forState:UIControlStateNormal];

  [self.displaySecondViewController sizeToFit];

  self.displaySecondViewController.center  = self.view.center;

 [self.displaySecondViewController addTarget:self  action:@selector (performDIsplaySecondViewController:) forControlEvents: UIControlEventTouchUpInside];

[self.view.addSubBiew: self.displaySecondViewController];

}

@end

现在创建第二个视图控制器,命名为SecondViewController,(就像创建FIrstViewController一样创建SecondViewController)既然创建了,那我们就给他一个标题

#import  “SecondViewController.h”

@implementation  SecondViewController

- (void)viewDidLoad

{

  [super viewDidLoad];

  self.title = @“Second Controller”;

}

两个视图都创建好了,接下来要做的就是实现从第一个视图跳转到第二个试图。使用popViewControllerAnimate:方法,取布尔作为参数。如果布尔值设置为YES ,就会在转到下一个视图控制器的时候会有动画效果,如果NO则反之。当显示屏幕上的第二个视图控制器,你会看到类似于图1-34所示的东西。

Implementing Navigation with UINavigationControlle

图:1-34

#import “SecondViewController


@implementation SecondViewController

- (void)viewDidLoad

{

   [super viewDidLoad];


   self.title = @“Second Controller”;

}


- (void)goBack

{

   [self.navigationController popViewControllerAnimates:YES];

}


- (void) viewDidAppear:(BOOL)paramAnimated

{

   [super viewDidAppear : paramAnimated];

   [self performSelector: @selector(goBack)  withObject:nil      afterDelay:5.0f];

}

@end


通过以上代码我们就完成了我们的需求了!



转载于:https://my.oschina.net/zboy/blog/200566