UIAlertView不能通过viewWithTag获取

问题

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

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

but a returns no object(0x0)

ANSWER 1

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

ANSWER 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.

你可能感兴趣的:(UIAlertView不能通过viewWithTag获取)