首页 > 其他 > 详细

IOS 7 Study - Implementing Navigation with UINavigationController

时间:2014-02-20 20:54:16      阅读:383      评论:0      收藏:0      [点我收藏+]

Problem
You would like to allow your users to move from one view controller to the other with
a smooth and built-in animation.


Solution
Use an instance of UINavigationController.

 

What it‘s like

If you’ve used an iPhone, iPod Touch, or iPad before, chances are that you have already
seen a navigation controller in action. For instance, if you go to the Settings app on your
phone and then press an option such as Wallpaper (see follow)

 

bubuko.com,布布扣

 

 1. Add UINavigationController property in app delegate implementation

bubuko.com,布布扣
#import "AppDelegate.h"
#import "FirstViewController.h"

@interface AppDelegate ()

@property (nonatomic, strong) UINavigationController *navigationController;

@end

@implementation AppDelegate

...
bubuko.com,布布扣

 

2. Initialize our navigation controller using its initWithRootViewController: method and pass our root view controller as its parameter.

    Then we will set the navigation controller as the root view controller of our window.

bubuko.com,布布扣
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    FirstViewController *viewController = [[FirstViewController alloc]
                                            initWithNibName:nil
                                            bundle:nil];
    self.navigationController = [[UINavigationController alloc]
                                 initWithRootViewController:viewController];
    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.navigationController;
    self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];
return YES; }
bubuko.com,布布扣

 

3. FirstViewController implementation

bubuko.com,布布扣
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@property (nonatomic, strong) UIButton *displaySecondViewController;

@end

@implementation FirstViewController

- (void) performDisplaySecondViewController:(id)paramSender {
    SecondViewController *secondController = [[SecondViewController alloc]
                                                initWithNibName:nil
                                                bundle:NULL];

    [self.navigationController pushViewController: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 addSubview:self.displaySecondViewController];
}

@end
bubuko.com,布布扣

 

4. create this second view controller, without a .xib file

bubuko.com,布布扣
#import "SecondViewController.h"

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Second Controller";
}

- (void) goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) viewDidAppear:(BOOL)paramAnimated {
    [super viewDidAppear:paramAnimated];
    [self performSelector:@selector(goBack)
        withObject:nil
        afterDelay:5.0f];
}
bubuko.com,布布扣

IOS 7 Study - Implementing Navigation with UINavigationController

原文:http://www.cnblogs.com/davidgu/p/3557126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!