Encode two ints into UIAlert view tag
// ---- Setup ----
// mask and shift: mask must be larger than any possible tag, shift must define the number of bits
#define TAG_MASK (0xF)
#define TAG_SHIFT (4)
#define TAG_MASK_COMP (~ TAG_MASK)
#define TAG_ENCODE(tag, val) (((val)<<TAG_SHIFT)|tag)
#define DECODE_TAG(mix) (mix&TAG_MASK)
#define DECODE_VAL(mix) ((mix&TAG_MASK_COMP)>>TAG_SHIFT)
// Normal style tags
#define ALERT_TAG_UPGRADE 0
#define ALERT_TAG_DOWNGRADE 1
#define ALERT_TAG_REMOVE 2
// ---- This shows how to encode ----
UIAlertView *alert; // replace with full alert init
// special value which we would like to pass along with tag
int val = 42;
int tag = ALERT_TAG_DOWNGRADE;
// encode the tag so to sent to the alert view
int mix = TAG_ENCODE( tag, val );
// attach mix to our alert and show
alert.tag = mix;
[alert show];
// ---- This shows how to decode ----
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
int tag_out = DECODE_TAG(alertView.tag);
int val_out = DECODE_VAL(alertView.tag);
// at this point tag_out is ALERT_TAG_DOWNGRADE and val_out is 42
}
Written by esromneb
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#