1

I hope to access UIAlertView using tag. The codes show below

UIAlertView *a=(UIAlertView *)[self.view  viewWithTag:presetTag]; 

but a returns no object(0x0)

I am looking to find a way to get the pointer to the UIAlertView object that is displayed without creating a reference to it in my UIViewController class that is displaying it. I am creating the UIAlertView and assigning it's tag property a constant non-zero value, then displaying it via show and releasing the UIAlertView reference.

An example where this could come in handy is if I want to hide the alert view based on some other event that is not touching one of the buttons on the alert view. Say a server informs the app that the alert is no longer valid and so I dismiss with button index -1 on the alert view. But, I have no reference to that alert so how can I find it?

Welcome any comment

Thanks

interdev

cynistersix
  • 1,215
  • 1
  • 16
  • 30
arachide
  • 8,006
  • 18
  • 71
  • 134

4 Answers4

4

As the UIAlertView is not part of the application's view hierarchy, the only way to access it would be to store the instance right after you created it, such as in a dictionary so you can later retrieve it.

Something like:

UIStateAlertView *alert = [[UIStateAlertView alloc]
                           initWithTitle:@"my_message"
                           message:nil
                           delegate:self
                           cancelButtonTitle:@"Cancel"
                           otherButtonTitles: nil];
alert.tag = kMY_TAG;
[_alertIndex setObject:alert forKey:[NSNumber numberWithInt:kMy_TAG]];
[alert show];
[alert release];

to retrieve the alert:

UIAlertView *alert = [_alertIndex objectForKey:[NSNumber numberWithInt:kMy_TAG]];

When you're done with the view, make sure to remove it from the dictionary:

[_alertIndex removeObjectForKey:[NSNumber numberWithInt:kMy_TAG]];

_alertIndex being a NSMutableDictionary

iPhone SDK: check if a UIAlertView is showing provides a solution, however this is relying on how Apple does its internal affair and could break at any moment; and so should be avoided

Community
  • 1
  • 1
jyavenard
  • 2,142
  • 1
  • 26
  • 35
2

UIAlertView creates its own UIWindow, which is not part of your app's main hierarchy of UIViews. Therefore it is not possible to find it using [UIView viewWithTag:].

You must store the pointer to the UIAlertViews you create in order to find them again later.

There is a way to access UIAlertViews in your app (and then you could use tags to verify that it's the one you're looking for), but it is relying on the internal structure of the app and may therefore stop working in future versions of iOS, though it is unlikely. If you are interested, please see this other SO response.

Community
  • 1
  • 1
squelart
  • 11,261
  • 3
  • 39
  • 43
-1

I think it is possible, please check that you are setting a non-zero value to the tag.

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
-2

Is this what you need? Specifically speaking, you can access each UIAlertView's tag info through its tag property in protocol callback methods.

@protocol UIAlertViewDelegate <NSObject>
@optional

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView;

- (void)willPresentAlertView:(UIAlertView *)alertView;  // before animation and showing view
- (void)didPresentAlertView:(UIAlertView *)alertView;  // after animation

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation

@end
Di Wu
  • 6,436
  • 3
  • 35
  • 51