self.navigationcontroller是nil吗?

有这个疑惑是因为在看别人的代码时遇到的一个疑问。
具体的代码是下面这样的。

/**
 * IoTAbout.m
 *
 * Copyright (c) 2014~2015 Xtreme Programming Group, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#import "IoTAbout.h"

@interface IoTAbout ()

@property (weak, nonatomic) IBOutlet UIImageView *imageAbout;

@end

@implementation IoTAbout

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"关于";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(onBack)];

    CGSize size = [UIApplication sharedApplication].keyWindow.frame.size;

    //iphone4s
    if(size.width == 320 && size.height <= 480)
    {
        self.imageAbout.image = [UIImage imageNamed:@"about.png"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这段代码是从机智云的开源app中找到的,它的主要功能是显示平常我们看到的“关于”这样的一个页面。在看这页面的代码时突然发现,在方法viewDidLoad()中竟然就直接给self.navigationItem 以及onback()中的self.navigationController 赋值和调用方法了。很奇怪,是在哪里给这两个指针附上对象的?
我曾尝试使用ViewController的presention的方法把上面的页面显示出来,但是失败了。首页显示是正常的,当点击按钮跳转后就一直黑屏了,没有期望的界面出现。然后就只好去搜了,关键字就是self.navigationController,于是搜到了一篇文章,里面说只有当把这个页面放入Navigation的stack之后,self.navigationController等指针才不为空,于是就是试了一下,还真是,果然如此。
下面上源码:

//
//  SecondViewController.h
//  test-navigation
//
//  Created by ChrisBluez on 9/27/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import 




@interface SecondViewController : UIViewController

@end
//
//  SecondViewController.m
//  test-navigation
//
//  Created by ChrisBluez on 9/27/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

    UILabel *label1 = [[UILabel alloc] init];
    label1.frame=CGRectMake(38, 80, 300, 30);
    label1.backgroundColor =  [UIColor whiteColor];
    label1.text = @"this is secondviewcontroller";
    [self.view addSubview:label1];

    NSLog(@"enter secondviewcontroller");

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame  = CGRectMake(38, 120, 300, 30);
    [btn1 setTitle:@"backTo" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(backto) forControlEvents:UIControlEventTouchUpInside];


    // Do any additional setup after loading the view.
    self.navigationItem.title = @"what the fuck";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"try" style:UIBarButtonItemStylePlain target:self action:@selector(backto)];


    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backto
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  AppDelegate.h
//  test-navigation
//
//  Created by ChrisBluez on 9/26/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

//
//  AppDelegate.m
//  test-navigation
//
//  Created by ChrisBluez on 9/26/16.
//  Copyright © 2016 ChrisBluez. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    ViewController *root1 = [[ViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:root1];
    self.window.rootViewController = nav1;




    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

    总感觉目前的学习方法是打一枪换一个地方,非常的不系统,唉,算了,就这样来吧。

你可能感兴趣的:(ios/osx)