简单使用iOS 关联引用objc_setAssociatedObject,objc_getAssociatedObject

使用关联引用


   #import 


    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_setAssociatedObject,objc_getAssociatedObject)