2012-01-15 21 views
0

我试图用ActivityIndi​​cator加载UIAlertView,当我点击一个按钮时弹出。在那一刻,我需要它运行dpkg命令。如何为系统命令显示“加载UIAlertView”?

我非常接近完成它。只有一个问题,当我触摸我的按钮时,UIAlertView不会在应用程序安装debian软件包时全部加载(灰色屏幕)。只要包装完成安装,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。所以不要讨厌。 :)建议?

回答

1

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

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

确保当您将警报分配给伊娃时,它会被正确保留。

现在,有一个事实的高层次问题是,您从看起来是iOS应用程序的系统命令中调用...但我只是假设您知道您在那里做什么...