【问题标题】:Indicator within alert, indicator doesn't show up (resolved using UIAlertController)警报中的指示器,指示器不显示(使用 UIAlertController 解决)
【发布时间】:2015-05-21 10:42:56
【问题描述】:

我正在尝试在 Xcode 6.3.2 上遵循来自 here 的公认解决方案,但指示器拒绝显示,或者由于某种原因我在 iPhone 6 plus 上看不到它。没有错误消息,带有标题等的警报显示和空白空间。

我做错了什么?按下应用上的连接按钮后会触发警报...

UIAlertView *alertWithInd;
UIActivityIndicatorView *alertInd;
alertWithInd = [[UIAlertView alloc] initWithTitle:@"Connecting..." message:@"Please wait...\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

alertInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
alertInd.frame=CGRectMake(0, 20, 270, 100);
[alertInd startAnimating];

[alertWithInd addSubview:alertInd];
[alertWithInd show];

【问题讨论】:

  • addSubview for UIAlertView 不再受支持。
  • 试试 UIAlertViewController
  • @MOHAMMADISHAQ,这有什么帮助?他也无法在那里添加指标。
  • 在带有 UIAlertViewController 的 Swift 中,我可以显示 UIPIcker,所以我也可以显示指示器吗?
  • 可以添加子视图..

标签: ios objective-c cocoa-touch uialertview uiactivityindicatorview


【解决方案1】:

在做了一些进一步的研究之后,对于任何有兴趣在一个地方找到解决方案的人(因为许多其他帖子中都有部分内容),我发布了使用 UIAlertController(而不是 UIAlert)的代码,有一个指示器,并且无需按下按钮即可解雇的代码。

UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:@"Connecting"
                                      message:@"Please wait...\n\n"
                                      preferredStyle:UIAlertControllerStyleAlert];


UIActivityIndicatorView *alertInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
alertInd.frame=CGRectMake(0, 40, 270, 100);
[alertInd startAnimating];

[alertController.view addSubview:alertInd];


id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
    rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alertController animated:YES completion:nil];

// to dismiss the alert with no button use:
[alertController dismissViewControllerAnimated:YES completion:nil];

【讨论】:

  • 完美运行!谢谢一堆:) 我唯一的问题是 alertInd 框架是否可以通过编程调整大小,而不是硬编码。虽然如果我们不能,这不是世界末日:)
【解决方案2】:

来自docs

子类化注释

UIAlertView 类旨在按原样使用,而不是 支持子类化。 这个类的视图层次是私有的,并且 不得修改。

【讨论】:

    【解决方案3】:

    我在我的一个项目中遇到了同样的问题。我已经以这种方式解决了这个问题 -

    在.h文件中定义如下

    @property (strong, nonatomic) UIAlertView *alertViewProcessing;
    

    .m 文件 -

    -(void)processing
    {
    
    
        self.alertViewProcessing = [[UIAlertView alloc] initWithTitle:@"Requesting...." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [indicator startAnimating];
    
    
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
        {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
            view.backgroundColor =[UIColor clearColor];
            indicator.frame = CGRectMake(120.0, 0.0, 20.0, 20.0 );
            [view addSubview:indicator];
    
            [self.alertViewProcessing setValue:view forKey:@"accessoryView"];
        }
        else
        {
            [self.alertViewProcessing setValue:indicator forKey:@"accessoryView"];
        }
    
        [self.alertViewProcessing show];
    
    }
    

    【讨论】:

    • 虽然这在技术上可能可行,但它违反了规则“此类的视图层次结构是私有的,不得修改。
    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多