0

我写了这段代码来定义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没有显示,有人知道错过了什么吗?提前致谢。

4

3 回答 3

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];
                });
于 2013-06-10T12:26:17.817 回答
1

您正在使用NSThread detachNewThreadSelector:to也 to your Show,问题是您现在有 2 个单独的用于显示警报,另一个用于关闭它。所以你无法确定哪个会先完成。所以也许第二个取消 UIAlertView 的线程首先完成,这将阻止显示。UIAlertViewdismissUIAlertViewNSThreadsThreadUIAlertView

尝试在UIAlertView不为每个方法创建新线程的情况下调用。

[self showAlertMethod2];
[self dismissAlertMethod2];
于 2013-06-10T13:28:48.757 回答
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];
}
于 2013-06-10T12:15:22.040 回答