經常遇到要給一個UIAlertView傳遞參數。
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"UIAlertView" message:nil
delegate:self
cancelButtonTitle:@"YES"
otherButtonTitles:@"NO",nil];
可以使用objc_setAssociatedObject,objc_getAssociatedObject
比如我們要傳遞一個參數NSDictionary*dic = @{@"123":@"456"};
static const char kRepresentedObject;
objc_setAssociatedObject(alert,
&kRepresentedObject,
dic,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSDictionary *dic = objc_getAssociatedObject(alertView,
&kRepresentedObject);
NSLog(@"%@",dic);
}
#import < objc/runtime.h >