【问题标题】:Is it possible to access UIAlertView using tag?是否可以使用标签访问 UIAlertView?
【发布时间】:2011-01-03 06:49:13
【问题描述】:

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

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

但是a没有返回对象(0x0)

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

一个可以派上用场的例子是,如果我想基于其他一些未触摸警报视图上的按钮之一的事件来隐藏警报视图。假设服务器通知应用程序警报不再有效,因此我在警报视图上使用按钮索引 -1 关闭。但是,我没有提到该警报,所以我该如何找到它?

欢迎评论

谢谢

交互开发

【问题讨论】:

    标签: iphone tags uialertview


    【解决方案1】:

    由于 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

    iPhone SDK: check if a UIAlertView is showing 提供了一个解决方案,但这取决于苹果公司内部事务的处理方式,随时可能中断;所以应该避免

    【讨论】:

      【解决方案2】:

      UIAlertView 创建自己的UIWindow,它不属于您的应用程序的主要层次结构UIViews。因此无法使用[UIView viewWithTag:] 找到它。

      您必须存储指向您创建的UIAlertViews 的指针,以便以后再次找到它们。

      有一种方法可以在您的应用程序中访问UIAlertViews(然后您可以使用标签来验证它是否是您要查找的),但它依赖于应用程序的内部结构,因此可能停止在未来版本的 iOS 中工作,尽管这不太可能。有兴趣请see this other SO response

      【讨论】:

        【解决方案3】:

        我认为有可能,请检查您是否为标签设置了非零值

        【讨论】:

          【解决方案4】:

          这是你需要的吗?具体来说,您可以通过协议回调方法中的tag属性访问每个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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-11
            • 2011-01-04
            相关资源
            最近更新 更多