警报内的指示灯,指示灯不显示(使用UIAlertController解决)

时间:2015-05-21 10:42:57

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

我试图按照here在Xcode 6.3.2上接受的解决方案,但该指示器拒绝显示,或者由于某种原因我无法在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];

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

来自docs

  

子类注释

     

UIAlertView类旨在按原样使用,但不是   支持子类化。 此类的视图层次结构是私有的   不得修改。

答案 2 :(得分:0)

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

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

}