UIAlertView傳遞參數

經常遇到要給一個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 >




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章