iOS runtime 关联对象(Associated Object)

  • ?需求

同一个类有多个alertView, 不同的alertView 点击确定按钮 执行的方法不同

alertOne 点击 确定按钮 执行 methodOne, alertTwo 点击确定按钮 执行 methodTwo

  • 常规做法

初始化并显示 alertOne

- (IBAction)showAlertOne:(id)sender {
    UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"AlertOne" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertOne setTag:1];
    [alertOne show];
}

初始化并显示alertTwo

- (IBAction)showAlertTwo:(id)sender {
    UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"AlertTwo" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertTwo setTag:2];
    [alertTwo show];
}

实现代理

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        return;
    }
    if (alertView.tag == 1) {
        if (buttonIndex == 1) {
            [self methodOne];
        }
    }
    else if (alertView.tag == 2) {
        if (buttonIndex == 1) {
            [self methodTwo];
        }
    }
}
  • Associated Object 做法

import runtime.h,定义属性关联key

#import "ViewController.h"
#import <objc/runtime.h>
static void *alertViewKey = "alertViewKey";

初始化并显示 alertOne,同时定义block,将block 与 alertOne 关联

- (IBAction)showAlertOne:(id)sender {
    UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"AlertOne" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [self methodOne];
        }
    };
    objc_setAssociatedObject(alertOne, alertViewKey, block, OBJC_ASSOCIATION_COPY);//将block 与 alertOne 关联
    [alertOne show];

初始化并显示 alertTwo,同时定义block,将block 与 alertTwo 关联

- (IBAction)showAlertTwo:(id)sender {
    UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"AlertTwo" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [self methodTwo];
        }
    };
    objc_setAssociatedObject(alertTwo, alertViewKey, block, OBJC_ASSOCIATION_COPY);
    [alertTwo show];
}

实现代理,在代理方法中,直接取出当前alert 所关联上的 block,再将buttonIndex 传入,让block 自己调用对应方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        return;
    }
    void(^block)(NSInteger) = objc_getAssociatedObject(alertView, alertViewKey);
    block(buttonIndex);
}
  • 比较

通过使用Associated object 方法有多个好处 :

简化代码

让代码层次清晰

充分利用对象与属性的拥有关系

时间: 2024-10-18 14:12:40

iOS runtime 关联对象(Associated Object)的相关文章

iOS objc_setAssociatedObject 关联对象的学习

今天看了FDTemplateLayoutCell的源码,类别里面相当频繁使用了关联对象,做笔记!!!学套路 主要函数: void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy); id objc_getAssociatedObject(id object, const void *key); void objc_removeAssociatedObjects

iOS中关联对象的简单使用objc_setAssociatedObject

首先看一下此方法接收的参数 objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) 被关联的对象,下面举的例子中关联到了UIAlertView 要关联的对象的键值,一般设置成静态的,用于获取关联对象的值 要关联的对象的值,从接口中可以看到接收的id类型,所以能关联任何对象 关联时采用的协议,有assign,retain,copy等协议,具体可以参考官方文档 下面就以

iOS runtime探究(四): 从runtiem开始实践Category添加属性与黑魔法method swizzling

你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639335 本文主要讲解runtime相关知识,从原理到实践,由于包含内容过多分为以下五篇文章详细讲解,可自行选择需要了解的方向: 从runtime开始: 理解面向对象的类到面向过程的结构体 从runtime开始: 深入理解OC消息转发机制 从runtime开始: 理解OC的属性property 从runtime开始: 实践Category添加属

Effective-OC 10.在既有类中使用关联对象存储自定义数据

EOC中介绍与案例 有时候需要在对象中存放相关的信息 这时候我们通常会从对象所属的类中继承一个子类,然后改用这个子类对象.然而并非所有的情况都能这么做.有的时候 类的实例可能是由某种机制创建的,而开发者无法令这种机制创建出自己写的子类的实例,OC中有一强大的特性可以解决这个问题 就是"关联对象" 可以给某对象关联许多其他的对象 这些对象通过"键"来区分.存储对象值得实惠 可以指明"存储策略",用以维护相对应的"内存管理语义".

外部获取IndexPath的几种方式(关联对象等)

1. 一般方式 - (void)buttonAction:(UIButton *)sender { UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview]; NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; NSLog(@"indexPath is = %i",indexPath.row); } 2.runtime添加属性方

iOS runtime实战应用:关联对象

在开始之前建议先阅读iOS runtime的基础理解篇:iOS内功篇:runtime 有筒子在面试的时候,遇到这样一个问题:"如何給NSArray添加一个属性(不能使用继承)",筒子立马蒙逼了,不能用继承,难道用分类?但是分类貌似只能添加方法不能添加属性啊,筒子百思不得其解,直到后来接触到了runtime才恍然大悟. 什么是关联对象 关联对象是指某个OC对象通过一个唯一的key连接到一个类的实例上.举个例子:xiaoming是Person类的一个实例,他的dog(一个OC对象)通过一根

iOS关联对象

Associated Objects(关联对象)或者叫作关联引用(Associative References),是作为Objective-C 2.0 运行时功能被引入到 Mac OS X 10.6 Snow Leopard(及iOS4)系统.与它相关在<objc/runtime.h>中有3个C函数,它们可以让对象在运行时关联任何值: OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value,

Runtime之成员变量&amp;属性&amp;关联对象

上篇介绍了Runtime类和对象的相关知识点,在4.5和4.6小节,也介绍了成员变量和属性的一些方法应用.本篇将讨论实现细节的相关内容. 在讨论之前,我们先来介绍一个很冷僻但又很有用的一个关键字:@encode 1.类型编码 为了协助运行时系统,编译器用字符串为每个方法的返回值.参数类型和方法选择器编码,使用的编码方案在其他情况下也很有用.在 Objective-C 运行时的消息发送机制中,传递参数时,由于类型信息的缺失,需要类型编码进行辅助以保证类型信息也能够被传递.在实际的应用开发中,使用案

IOS Runtime属性关联实现表格编辑文本

要实现在表格里编辑文本, 表格让我想到了CollectionView,文本让我想起TextView, 做之前想了好久怎么样来获得编辑的是哪个TextView,要获取对应的IndexPath啊,想着之前Cell中的按钮用block来实现,在自定义的Cell中加一个属性存IndexPath,可想着就一个TextView要自定义写一个类这样也未免太麻烦了.正好突然想到之前听过的属性管理,自己就凑着这个机会用了下,赶脚还不错. 有时候类的实例可能是某种机制所创建,而开发者无法令这种机制创建出自己所写的子