Can a UIAlertView pass a string and an int through a delegate

后端 未结 1 1846
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 11:42

I have a UIAlertView (several, in fact), and I\'m using the method -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex to trigger

1条回答
  •  旧时难觅i
    2021-02-01 12:15

    Probably the quickest way to associate an object with an arbitrary other object is to use objc_setAssociatedObject. To use it correctly, you need an arbitrary void * to use as a key; the usual way to do that is to declare a static char fooKey globally in your .m file and use &fooKey as the key.

    objc_setAssociatedObject(alertView, &secretStringKey, mySecretString, OBJC_ASSOCIATION_RETAIN);
    objc_setAssociatedObject(alertView, &intKey, [NSNumber numberWithInt:myInt], OBJC_ASSOCIATION_RETAIN);
    

    Then use objc_getAssociatedObject to retrieve the objects later.

    NSString *mySecretString = objc_getAssociatedObject(alertView, &secretStringKey);
    int myInt = [objc_getAssociatedObject(alertView, &intKey) intValue];
    

    Using OBJC_ASSOCIATION_RETAIN, the values will be retained while attached to the alertView and then automatically released when alertView is deallocated.

    0 讨论(0)
提交回复
热议问题