美文网首页
iOS Runtime 学习之关联对象的经典例子电话簿

iOS Runtime 学习之关联对象的经典例子电话簿

作者: dragonYao | 来源:发表于2017-02-24 17:27 被阅读23次

曾在之前的一个项目的需求中要求:

  • 我弹出一个电话列表,点击cell上的“拨打”Button,弹出一个AlertView
  • alert的message显示电话号码
  • 点击“确定”就给对应的电话号码打电话
  • 点击“取消”就让AlertView消失。
讲讲我之前解决这个需求的方法
  • 我当时用的是比较麻烦的方法做了一个障眼法是点击一个cell(并不是点击button 😄)
  • 保存其indexPath,然后点击了alertView的确定按钮之后,通过indexPath.section拿到在那个组取出model数据中的数组
  • 然后在通过indexPath.row拿到这个电话号码,再去调用打电话的方法,比较麻烦,还要写两遍取电话号的Code。
接触runtime之后,发现有更简单的方法去完成需求。Talks no use show me the code。上代码:RuntimeDemo 先看一下效果
1111.gif
  • 先定义一个cell(自己根据UI样式定义)我用Xib拖了
// cell的.h文件
#import <UIKit/UIKit.h>
typedef void(^CallBackBlack)(UIButton *button);

@interface TestCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UIButton *callButton;
@property (nonatomic, copy) CallBackBlack callBlack;

@end

//cell的.m文件代码
#import "TestCell.h"

@implementation TestCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (IBAction)buttonAction:(id)sender {

    UIButton *button = (UIButton *)sender;
    if (self.callBlack) {
        self.callBlack(button);
    }
}

@end

  • 上面cell的代码相当的简单吧 O(∩_∩)O哈哈~, 下面我们看一下具体的使用(这里我就用一组假数据数据代替了):
#import "TestTableViewController.h"
#import "TestCell.h"
#import <objc/runtime.h>

@interface TestTableViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) NSArray *dataItems;

@end

static NSString *const kSourceKey = @"SourceKey";

@implementation TestTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataItems = @[@"18301267512", @"18701311556", @"15845429832"];

    //register cell
    [self.tableView registerNib:[UINib nibWithNibName:@"TestCell" bundle:nil] forCellReuseIdentifier:@"TestCell"];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataItems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell"];
    NSString *phoneNumber = _dataItems[indexPath.row];
    cell.numberLabel.text = phoneNumber;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    cell.callBlack = ^(UIButton *button) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"拨打:" message:phoneNumber delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        //绑定
        objc_setAssociatedObject(alertView, &kSourceKey, phoneNumber, OBJC_ASSOCIATION_COPY_NONATOMIC);
        [alertView show];
    };
#pragma clang diagnostic pop
    return cell;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
#pragma clang diagnostic pop
    //取出
    NSString *numberStr = objc_getAssociatedObject(alertView, &kSourceKey);
    if (buttonIndex == 0) {
        NSString *callString = [NSString stringWithFormat:@"telprompt://%@", numberStr];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callString]];
    }
}
@end

说明一下:
  • 上边用到runtime的两个方法一个是 void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) 绑定PhoneNumber和AlertView
  • 另一个是 id objc_getAssociatedObject(id object, const void *key) 通过绑定的alertView对象获取电话号码, 相当方便吧。
以上如有不正确的地方,请多多指教哈 😀。再次贴上demoRuntimeDemo

相关文章

  • iOS Runtime 学习之关联对象的经典例子电话簿

    曾在之前的一个项目的需求中要求: 我弹出一个电话列表,点击cell上的“拨打”Button,弹出一个AlertVi...

  • iOS~runtime之关联对象

    什么是runtime? RunTime又叫运行时。OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消...

  • iOS runtime 关联对象

    最近在看runtime的相关知识,感觉里面东西还蛮多的,反正感觉runtime被戴上了很多高大上的帽子。查看了很多...

  • iOS runtime 关联对象

    关联对象 Associated Object 顾名思义,就是把一个对象关联到另外一个对象身上。 关于关联对象,ru...

  • iOS runtime之关联对象AssociatedObject

    在分类中添加属性,使用对象关联来实现: 例子:创建一个label的分类 #import@interfaceUILa...

  • iOS Runtime特性之关联对象

    前言 现在你准备用一个系统的类或者是你写的类,但是这个类并不能满足你的需求,你需要额外添加一个属性。一般解决办法要...

  • iOS Runtime之四:关联对象

    一、概述 如何给NSArray添加一个属性(不能使用继承)?不能用继承,难道用分类?但是分类只能添加方法不能添加属...

  • iOS Runtime 之关联对象AssociatedObjec

    面试中总该会有人问到关于runtime的问题,虽已不是个纯native开发者,但还是有必要去整理一下以前的笔记,讲...

  • Effective Objective-C 2.0 第二章 十、

    我之前已经在这篇文章iOS runtime 关联对象做了详细介绍。本篇只是简单介绍下。 创建关联对象: id ob...

  • runtime 之关联对象

    如何给一个NSArray添加一个属性,不能使用继承。 分类不能添加属性,只能添加方法。这时就可以使用关联对象。 关...

网友评论

      本文标题:iOS Runtime 学习之关联对象的经典例子电话簿

      本文链接:https://www.haomeiwen.com/subject/sauwwttx.html