Indicator within alert,indicator 不显示(使用 UIAlertController 解决)

Indicator within alert, indicator doesn't show up (resolved using UIAlertController)

我正在尝试遵循 here 在 Xcode 6.3.2 上接受的解决方案,但指示器拒绝显示或者我在 iPhone 上看不到它6加上一些原因。没有错误消息,带有标题的警报等显示为空 space.

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

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];

来自 docs

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

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

在 .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];

}

在做了一些进一步的研究之后,对于任何有兴趣在一个地方找到解决方案的人(因为许多其他 posts 中都有部分)我 post 使用 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];