使用关联引用
#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);
}