iOS开发-通过 objc_setAssociatedObject alert 和 button关联 及传值
2015-08-15 11:36
537 查看
/唯一静态变量key
static const char associatedkey;
static const char associatedButtonkey;
- (IBAction)sendAlert:(id)sender
{
NSString *message =@"我知道你是按钮了";
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"我要传值"
delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
alert.delegate =self;
[alert show];
//#import <objc/runtime.h>头文件
//objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。
//1 源对象alert
//2 关键字 唯一静态变量key associatedkey
//3 关联的对象 sender
//4 关键策略 OBJC_ASSOCIATION_RETAIN_NONATOMIC
objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(alert, &associatedButtonkey, sender,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//通过 objc_getAssociatedObject获取关联对象
NSString *messageString =objc_getAssociatedObject(alertView, &associatedkey);
UIButton *sender = objc_getAssociatedObject(alertView, &associatedButtonkey);
_labebutton.text = [[sender titleLabel]text];
_ThisLabel.text = messageString;
//使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。
}
static const char associatedkey;
static const char associatedButtonkey;
- (IBAction)sendAlert:(id)sender
{
NSString *message =@"我知道你是按钮了";
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"我要传值"
delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
alert.delegate =self;
[alert show];
//#import <objc/runtime.h>头文件
//objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。
//1 源对象alert
//2 关键字 唯一静态变量key associatedkey
//3 关联的对象 sender
//4 关键策略 OBJC_ASSOCIATION_RETAIN_NONATOMIC
objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(alert, &associatedButtonkey, sender,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//通过 objc_getAssociatedObject获取关联对象
NSString *messageString =objc_getAssociatedObject(alertView, &associatedkey);
UIButton *sender = objc_getAssociatedObject(alertView, &associatedButtonkey);
_labebutton.text = [[sender titleLabel]text];
_ThisLabel.text = messageString;
//使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。
}
相关文章推荐
- WordPress固定链接修改报错:Object not found的解决方法
- Objective-C:三种文件导入的方式比较
- QAxObject操作Excel表格
- Effective Objective-C Notes:GCD 实现同步锁
- iOS开发:Objective-C精确的货币计算
- 《Objective-C基础教程》第12章 类别
- Objective-C 苹果开发文档 04 Encapsulating Data
- C++对象模型——对象复制语意学 (Object Copy Semantics)(第五章)
- '[<NSObject 0x8a4b500> setValue:forUndefinedKey:]
- Objective-C总Runtime的那点事儿(一)消息机制
- Objective-C:保留计数器思想的详解(对象的保留和所有权的释放)
- OC - ClassAndObject
- Objective-C 循环引用的处理
- Objective-C #include、#import 、 @class的使用区别
- ObjectiveC运行时类型
- Objective-C:深复制(拷贝)
- Objective-C:浅复制(拷贝)
- -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法
- Objective-C:对象之间循环引用会造成死循环
- JS 对象(Object)和字符串(String)互转