美文网首页
简单使用iOS 关联引用objc_setAssociatedOb

简单使用iOS 关联引用objc_setAssociatedOb

作者: abyte | 来源:发表于2016-01-11 10:33 被阅读530次

    使用关联引用

    
       #import <objc/runtime.h>
    
    
        NSString *interestingString = @"My Interesting Thing";
        //将数据和控件绑定
        UIAlertView *alert          = [[UIAlertView alloc] initWithTitle:@"Alert" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        objc_setAssociatedObject(alert, &kRepresentedObject, interestingString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [alert show];
    
    

    在代理方法中取值

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSString *interestingString = objc_getAssociatedObject(alertView, &kRepresentedObject);
        NSLog(@"%@", interestingString);
    }
    

    相关文章

      网友评论

          本文标题:简单使用iOS 关联引用objc_setAssociatedOb

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