I'm using a UIAlertView + block Category found here.
I'm having trouble following how he has created the blocks:
typedef void (^DismissBlock)(int buttonIndex);
I understand this, use it on creating my own blocks. So I created mine:
typedef void (^DismissBlockWithView)(UIAlertView *alertview, int buttonIndex);
I think I understand what is happening with the setter method:
- (void)setDismissBlock:(DismissBlock)dismissBlock
{
objc_setAssociatedObject(self, &DISMISS_IDENTIFER, dismissBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
From what I understand, this is a lower level way to set a variable. I'm not sure where, when, or why it should be used. Please correct me if I'm mistaken. It is setting the referenced value of DIMISS_IDENTIFER to the dismissBlock object.
So for my block:
-(void)setDismissBlockWithView:(DismissBlockWithView)dismissBlockWithView {
objc_setAssociatedObject(self, &ALERT_VIEW, dismissBlockWithView, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &DISMISS_IDENTIFER, dismissBlockWithView, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
I set each variable.
Now the part I'm having trouble understanding is the getter method:
- (DismissBlock)dismissBlock
{
return objc_getAssociatedObject(self, &DISMISS_IDENTIFER);
}
So the objc_getAssociatedObject() takes in id object and const void *key variables. Can you only have a single variable when creating blocks in this manner? I know I don't know the underlying logic behind all this, just kinda trying to use common sense logic to put it all together.
What do I need to do to change it so I can have the UIAlertView and the buttonIndex returned?
I want the UIAlertView returned because I am taking in user input (using the UIAlertViewStylePlainTextInput style option) through it and the text is stored in the alert view.
@property (copy) DismissBlockWithView dismissBlock;?