Currently I've got a class popping up UIAlertView
s here and there. Currently, the same class is the delegate for these (it's very logical that it would be). Unfortunately, these UIAlertView
s will call the same delegate methods of the class. Now, the question is - how do you know from which alert view a delegate method is invoked? I was thinking of just checking the title of the alert view, but that isn't so elegant. What's the most elegant way to handle several UIAlertView
s?

- 8,165
- 6
- 62
- 81

- 18,812
- 25
- 97
- 108
5 Answers
Tag the UIAlertView
s like this:
#define kAlertViewOne 1
#define kAlertViewTwo 2
UIAlertView *alertView1 = [[UIAlertView alloc] init...
alertView1.tag = kAlertViewOne;
UIAlertView *alertView2 = [[UIAlertView alloc] init...
alertView2.tag = kAlertViewTwo;
and then differentiate between them in the delegate methods using these tags:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == kAlertViewOne) {
// ...
} else if(alertView.tag == kAlertViewTwo) {
// ...
}
}

- 109,922
- 25
- 130
- 137
-
3Ah, nice. Though I'd use a switch. :) – quano Feb 26 '10 at 07:47
-
1of course a switch would work too, but I never liked switch. =) – Can Berk Güder Feb 26 '10 at 11:41
-
Great answer. But that leads me to another question. Why do you define the kAlertViewOne 1 and kAlertViewRwo 2. Couldn't you just use in the alertView.tag = 1 or alertView.tag = 2? Is this done for a reason? – George Feb 26 '10 at 16:29
-
3kAlertViewOne and kAlertViewTwo make more sense than 1 or 2. for example, in your question, you can use kAlertResume and kAlertRetry, and they'd be more readable than two random numbers. – Can Berk Güder Feb 26 '10 at 16:31
-
Is there a reason you would not have them strings? – George Feb 26 '10 at 16:33
-
tag is defined as an NSInteger. – Can Berk Güder Feb 26 '10 at 16:42
-
1FYI, assigning tags the numbers less than 10 might conflict with those Apple rarely uses. – Özgür Sep 06 '10 at 08:23
-
Incidentally you can cast a NSString * into a NSInteger and read it back later. However that may be too kludgy for some. – futureelite7 Sep 07 '10 at 06:30
FYI, if you want to target just iOS 4 users (which is reasonable now that ~98.5% of clients have at least iOS 4 installed), you should be able to use Blocks to do really nice inline handling of UIAlertViews.
Here's a Stackoverflow question explaining it:
Block for UIAlertViewDelegate
I tried using Zachary Waldowski's BlocksKit framework for this. His UIAlertView(BlocksKit) API reference looked really good. However, I tried to follow his instructions to import the BlocksKit framework into my project, but unfortunately I couldn't get it to work.
So, as Can Berk Güder suggests, I've used UIAlertView
tags for now. But at some point in future I'm going to try to move to using Blocks (preferably one which supports ARC out of the box)!
-
If you're using iOS 8+, UIAlertController has block based delegate methods. – Marcus Adams Aug 11 '15 at 14:32
easier & newer
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
// first alert...
} else {
// sec alert...
}
}
all done!

- 158
- 2
- 9
You can overcome this whole ordeal and prevent yourself from using tags by enhancing UIAlertView to use block callbacks. Check out this blog post I wrote on the subject.

- 14,244
- 5
- 52
- 80
I've always thought using tags is a bit of a hack. If you do use them, at least set some defined constants for the tag numbers.
Instead, I use properties like this:
In the interface section:
@property (nonatomic, weak) UIAlertView *overDueAlertView;
@property (nonatomic, weak) UIAlertView *retryPromptAlertView;
Creating the alert view:
UIAlertView *alert = [[UIAlertView alloc] init...
self.overDueAlertView = alert;
[alert show];
Delegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == self.overDueAlertView) {
// Overdue alert
} else if (alertView == self.retryPromptAlertView) {
// Retry alert
}

- 53,009
- 9
- 91
- 143