0

我试图让带有 ActivityIndi​​cator 的 Loading UIAlertView 在我点击按钮后立即弹出。在那一刻,我需要它来运行dpkg命令。

我非常接近完成它。只有一个问题,当我触摸我的按钮时,当应用程序安装 debian 包时,UIAlertView 不会一直加载(屏幕变暗)。包安装完成后,UIAlertView 会一直加载一秒钟。然后被解雇[alert dismissWithClickedButtonIndex:0 animated:YES];

我不确定这是否需要在另一个线程上,所以我尝试这样做。不确定我是否设置正确。所以这是我的代码。建议?更正?

.m

-(IBAction)installdeb:(id)sender{
    UIAlertView *alerty = [[UIAlertView alloc] initWithTitle:@"Installing..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [alerty addSubview:progress];
    [progress startAnimating];
    [alerty show];
    [alerty release];
    [NSThread detachNewThreadSelector:@selector(installdeb) toTarget:self withObject:nil];
}

- (void)installdeb{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    char *installdebchar = [[NSString stringWithString:@"dpkg -i /Applications/MyApp.app/package.deb"] UTF8String];
    system(installdebchar);
    if (system(installdebchar) == 0){
        [alerty dismissWithClickedButtonIndex:0 animated:YES];
        UIImage *img1 = [UIImage imageNamed:@"whitecheckmark.png"];
        [ImageView1 setImage:img1];
    } else {
        [alerty dismissWithClickedButtonIndex:0 animated:YES];
    }
    [pool release];
}

。H

@class DebInstallViewController;

@interface DebInstallViewController : UIViewController <UINavigationBarDelegate, UINavigationControllerDelegate, UIAlertViewDelegate>{

    IBOutlet UIAlertView *alert;

    IBOutlet UIImageView *ImageView1;

}

- (IBAction)installdeb:(id)sender;

@end

我对目标c有点陌生。所以不要恨。:) 建议?

4

1 回答 1

1

看起来你正在采取正确的整体方法。但是,有几个问题。首先,不清楚“installdeb”中的“alerty”来自哪里。我假设您打算使用成员变量“警报”?

假设是这种情况,我可以在您的代码中看到的主要低级问题是您试图在后台线程上调用dismissWithClickedButtonIndex:animated:。Apple 的文档声明所有 UIKit 交互都必须发生在主线程上,除非另有明确说明。

确保在分配警报 ivar 时它得到正确保留。

现在,您正在从看起来像 iOS 应用程序的程序中调用系统命令这一事实存在高级问题……但我假设您知道自己在做什么……

于 2012-01-15T08:38:27.307 回答