Implementing Navigation with UINavigationControlle

问题:objective-c

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

解决方法:ide

     

     使用UINavigationController动画

讨论:atom

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

     基本文件搞定,而后咱们开始,找到APP的代理文件 AppDelegate.m文件,而后写下以下代码:.net

                 #import “AppDelegate.h”代理

                 #import “FirstViewController.h”orm

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

}

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

图: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所示的东西。

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


经过以上代码咱们就完成了咱们的需求了!

相关文章
相关标签/搜索