自定义的导航控制器

作者:计算机网络

// EFNavigationController.h// JueyingWeibo//// Created by Jueying on 15/3/4.// Copyright 2015年 MyCompany. All rights reserved.//

NSObject的hash方法

 @interface EFNavigationController : UINavigationController @end

澳门新萄京赌场手机版 1

// EFNavigationController.m// JueyingWeibo//// Created by Jueying on 15/3/4.// Copyright 2015年 MyCompany. All rights reserved.//

 

 #import "EFNavigationController.h" @interface EFNavigationController () @end @implementation EFNavigationController

说明

当EFNavigationController这个类第一次加载的时候,只调用一次 initialize,这个方法是当第一次给这个类发送消息时调用一次

本示例仅仅演示一个对象什么时候执行hash方法。

   initialize { // 设置导航控制器barButtonItem的title颜色 [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateNormal]; [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateHighlighted]; [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateDisabled]; } - viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self leftTouchBack]; } - didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }#pragma mark - Pan gestureRecognizer- leftTouchBack { // 获取系统自带滑动手势的target对象 id target = self.interactivePopGestureRecognizer.delegate;#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wundeclared-selector" UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];#pragma clang diagnostic pop pan.delegate = self; [self.view addGestureRecognizer:pan]; self.interactivePopGestureRecognizer.enabled = NO; _panGesture = pan; // 创建一个成员变量,可以在禁止手势的地方直接调用_panGesture.enable = NO;来禁用手势。}- gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if (self.childViewControllers.count == 1) { return NO; } return YES;} #pragma mark - 重写push方法 - pushViewController:(UIViewController *)viewController animated:animated { if (self.viewControllers.count > 0) {//除了根视图控制器以外的所有控 制,以压栈的方式push进来,就隐藏tabbar viewController.hidesBottomBarWhenPushed = YES; viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithNormal:@"navigationbar_back" highlighted:@"navigationbar_back_highlighted" target:self action:@selector]; viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithNormal:@"navigationbar_more" highlighted:@"navigationbar_more_highlighted" target:self action:@selector(popToRoot)]; } [super pushViewController:viewController animated:animated]; } - back { [self popViewControllerAnimated:YES]; } - popToRoot { [self popToRootViewControllerAnimated:YES]; } @end

 

细节

  1. 必要的Model类,重载了hash方法用以反映Hash方法是否被调用了

澳门新萄京赌场手机版 2

澳门新萄京赌场手机版 3

  1. 测试

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model *model = [Model new];

      [model hash]; model = nil; }

      @end

澳门新萄京赌场手机版 4

  1. 测试 isEqual: 方法执行的时候是否会执行 hash 方法,打印情况里面是没有的

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model modelA = [Model new]; Model modelB = [Model new];

      if ([modelA isEqual:modelB]) {

        NSLog(@"YES");
      

      } else {

        NSLog(@"NO");
      

      } }

      @end

澳门新萄京赌场手机版 5

  1. 用 NSMutableSet 添加对象,这时候会执行hash方法,至于为何会执行2回 _(:з」∠)_ ?

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model model = [Model new]; NSMutableSet set = [NSMutableSet set];

      [set addObject:model]; }

      @end

澳门新萄京赌场手机版 6

  1. 用 NSMutableArray 添加对象测试一下,发现不会执行 hash 方法

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model model = [Model new]; NSMutableArray array = [NSMutableArray array];

      [array addObject:model]; }

      @end

澳门新萄京赌场手机版 7

  1. 用作 NSMutableDictionary 中的 object 时,hash 方法不会执行

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model model = [Model new]; NSMutableDictionary 澳门新蒲京www66126cc,dictionary = [NSMutableDictionary dictionary];

      [dictionary setObject:model forKey:@"A"]; [dictionary objectForKey:@"A"]; }

      @end

澳门新萄京赌场手机版 8

7. 用作 NSMutableDictionary 中的 key 时,hash 方法执行了,不过崩溃了,因为 Model 类没有实现 NSCopying 协议

//
//  ViewController.m
//  Hash
//
//  Created by YouXianMing on 16/4/15.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    Model               *model      = [Model new];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    [dictionary setObject:@"A" forKey:model];
}

@end

澳门新萄京赌场手机版 9

  1. NSSet 在初始化的时候添加了 model 并不会让 model 执行 hash 方法

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model *model = [Model new];

      NSSet *set = [NSSet setWithObjects:model, nil];

      if ([[set anyObject] isEqual:model]) {

        NSLog(@"A");
      

      }

      set = nil; }

      @end

澳门新萄京赌场手机版 10

  1. 在创建不可变数组时,model 作为 key 会执行 hash 方法,但同样会崩溃,因为 Model 类没有实现 NSCopying 协议

    // // ViewController.m // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. //

    #import "ViewController.h" #import "Model.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

      [super viewDidLoad];

      Model model = 澳门新萄京赌场手机版,[Model new]; NSDictionary dictionary = @{model : @"A"}; dictionary = nil; }

      @end

澳门新萄京赌场手机版 11

 

总结

一个对象在用作key值时,其 hash 方法会被调用,用以生成一个唯一标识符,NSDictionary 需要根据唯一 key 值(根据 hash 算法生成的值)查找对象, NSSet 需要根据 hash 值来确保过滤掉重复的对象。

本文由新葡京8455发布,转载请注明来源

关键词: