尽管我添加了show,但无法在iPhone中显示UIAlertView

时间:2013-06-10 11:38:09

标签: iphone ios ipad uialertview

我写了这段代码来定义UIAlertView

-(void)showAlertMethod2 {
progressAlert2 = [[UIAlertView alloc] initWithTitle:@"تتم المزامنة..يرجى الانتظار ...\n" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
CGRect alertFrame = progressAlert2.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(135,alertFrame.size.height+55, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating];
[progressAlert2 addSubview:activityIndicator];
[progressAlert2 show];
}

-(void)dismissAlertMethod2
{
[progressAlert2 dismissWithClickedButtonIndex:0 animated:YES];
}

我在这里打电话:

[NSThread detachNewThreadSelector:@selector(showAlertMethod2) toTarget:self withObject:nil];

[self performSelector:@selector(syncToServer) withObject:nil];
[self performSelector:@selector(syncFromServer) withObject:nil]

[NSThread detachNewThreadSelector:@selector(dismissAlertMethod2) toTarget:self withObject:nil];

syncToServer是一种将数据同步到服务器的方法,syncFromServer是将数据同步到服务器

问题是UIAlertView没有显示,有谁知道错过了什么? 提前谢谢。

3 个答案:

答案 0 :(得分:2)

我通过以下代码解决了

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self showAlertMethod2];
                });

                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncFromServer) withObject:nil];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissAlertMethod2];
                });

答案 1 :(得分:1)

您正在使用NSThread detachNewThreadSelector: Show UIAlertView dismiss UIAlertView NSThreads,您现在有2个单独的Thread一个用于显示警报,另一个用于解除警报。因此,您无法确定哪个UIAlertView将首先完成。所以也许第二个线程首先解雇UIAlertView,这将阻止UIAlertView显示。

尝试调用[self showAlertMethod2]; [self dismissAlertMethod2]; 而不为每个方法创建新的Thread。

{{1}}

答案 2 :(得分:0)

-(void)showAlertMethod2 {
    progressAlert2 = [[UIAlertView alloc] initWithTitle:@"تتم المزامنة..يرجى الانتظار ...\n" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    CGRect alertFrame = progressAlert2.frame;
    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(135,alertFrame.size.height+55,    alertFrame.size.width,30);
    activityIndicator.hidden = NO;
    activityIndicator.contentMode = UIViewContentModeCenter;
    [activityIndicator startAnimating];
    [progressAlert2 addSubview:activityIndicator];
    [progressAlert2 show];
}

-(void)dismissAlertMethod2
{
    [progressAlert2 dismissWithClickedButtonIndex:0 animated:YES];
}

这是你的方法就好了。

现在称他们为

-(void)syncBothWay{
    [self showAlertMethod2];
    [self performSelector:@selector(syncToServer) withObject:nil afterDelay:0.0];
    [self performSelector:@selector(syncFromServer) withObject:nil afterDelay:0.0];
}

-(void)syncToServer{
    //sync
}

-(void)syncFromServer{
    //sync
    after sync completed call [self dismissAlertMethod2];
}