曾在之前的一个项目的需求中要求:
- 我弹出一个电话列表,点击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对象获取电话号码, 相当方便吧。