2012-01-15 35 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應用程序的系統命令中調用...但我只是假設您知道您在那裏做什麼...