Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 是否可以使用标记访问UIAlertView?_Iphone_Tags_Uialertview - Fatal编程技术网

Iphone 是否可以使用标记访问UIAlertView?

Iphone 是否可以使用标记访问UIAlertView?,iphone,tags,uialertview,Iphone,Tags,Uialertview,我希望使用标签访问UIAlertView。代码如下所示 UIAlertView *a=(UIAlertView *)[self.view viewWithTag:presetTag]; 但a不返回任何对象(0x0) 我希望找到一种方法来获取指向显示的UIAlertView对象的指针,而无需在显示它的UIViewController类中创建对它的引用。我正在创建UIAlertView并为其标记属性指定一个常量非零值,然后通过show显示它并释放UIAlertView引用 例如,如果我想基于某

我希望使用标签访问UIAlertView。代码如下所示

UIAlertView *a=(UIAlertView *)[self.view  viewWithTag:presetTag]; 
但a不返回任何对象(0x0)

我希望找到一种方法来获取指向显示的UIAlertView对象的指针,而无需在显示它的UIViewController类中创建对它的引用。我正在创建UIAlertView并为其标记属性指定一个常量非零值,然后通过show显示它并释放UIAlertView引用

例如,如果我想基于某个其他事件隐藏警报视图,而该事件未触及警报视图上的某个按钮,则此操作可能会派上用场。假设服务器通知应用程序警报不再有效,因此我在警报视图中使用按钮索引-1解除警报。但是,我没有提及该警报,因此我如何才能找到它

欢迎评论

谢谢


interdev

这就是您需要的吗?具体来说,您可以通过协议回调方法中的标记属性访问每个UIAlertView的标记信息

@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
@协议UIAlertViewDelegate
@可选的
//单击按钮时调用。此调用返回后,视图将自动取消
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引;
//取消视图时调用(例如,用户单击“主页”按钮)。当用户单击“取消”按钮时,不会调用此函数。
//如果未在委托中定义,则模拟单击“取消”按钮
-(void)alertView取消:(UIAlertView*)alertView;
-(void)willPresentAlertView:(UIAlertView*)alertView;//在动画和显示视图之前
-(void)didPresentAlertView:(UIAlertView*)alertView;//动画之后
-(void)alertView:(UIAlertView*)alertView将使用buttonIndex:(NSInteger)buttonIndex解除警报;//在动画和隐藏视图之前
-(void)alertView:(UIAlertView*)alertView使用buttonIndex:(NSInteger)buttonIndex进行了删除;//动画之后
@结束

我认为这是可能的,请检查您是否正在为标签设置一个非零值
UIAlertView
创建自己的
UIWindow
,它不是应用程序的
UIView
主层次结构的一部分。因此,使用
[UIView viewWithTag:::
无法找到它

您必须存储指向所创建的
UIAlertView
s的指针,以便稍后再次找到它们


有一种方法可以访问应用程序中的
UIAlertView
s(然后您可以使用标签验证它是否是您正在寻找的),但它依赖于应用程序的内部结构,因此可能会在未来版本的iOS中停止工作,尽管可能性不大。如果您感兴趣,请。

由于UIAlertView不是应用程序视图层次结构的一部分,因此访问它的唯一方法是在创建实例后立即将其存储在字典中,以便以后检索

比如:

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];
要检索警报,请执行以下操作:

UIAlertView *alert = [_alertIndex objectForKey:[NSNumber numberWithInt:kMy_TAG]];
处理完视图后,请确保将其从字典中删除:

[_alertIndex removeObjectForKey:[NSNumber numberWithInt:kMy_TAG]];
_alertIndex是一个
NSMutableDictionary

提供了一个解决方案,但这取决于苹果如何处理其内部事务,并可能随时中断;因此应该避免