2010-06-11 34 views
0

我有一個命令,應該在特定的時間密集代碼執行之前加載子視圖。然而,該命令運行,及時的代碼執行,然後子視圖出現。有什麼我可以解決這個問題嗎?代碼執行前ipad子視圖不加載

progressViewController = [[ProgressView alloc] initWithNibName:@"ProgressView" bundle:[NSBundle mainBundle]]; 
[self.view addSubview:[progressViewController view]]; 



NSString *[email protected]"guy"; 
NSString *encodedName =[[NSString alloc] init]; 

int asci; 

for(int i=0; i < [name length]; i++) 
{ 
    //NSLog(@"1"); 
     asci = [name characterAtIndex:i]; 
    NSString *str = [NSString stringWithFormat:@"%d,", asci]; 
     encodedName =[encodedName stringByAppendingFormat: str]; 
} 

NSString *urlString = [NSString stringWithFormat:@"someurl.com"]; 

NSURL *theUrl = [[NSURL alloc] initWithString:urlString]; 

NSString *result=[NSString stringWithContentsOfURL:theUrl]; 

result = [result substringFromIndex:61]; 
result = [result substringToIndex:[result length] - 20]; 

NSLog(result); 


outLab.text=result; 


[[progressViewController view] removeFromSuperview]; 

回答

0

1)儘量 [self.view setNeedsUpdating] 添加子視圖

2) 嘗試分裂時間密集到另一個線程後....

- (void) f 
{ 
// executed in main UI thread 
progressViewController = [[ProgressView alloc] initWithNibName:@"ProgressView" bundle:[NSBundle mainBundle]]; 
[self.view addSubview:[progressViewController view]]; 

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

} 

- (void) doCompute 
{ 
// in a different thread....so we need a autoreleasepool 
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; 
    // do your computation here... 

[self performSelectorOnMainThread:@selector(taskComplete:) 
           withObject:result waitUntilDone:NO]; 

[autoreleasepool release]; 

} 

- (void) taskComplete 
{ 
    // executed in UI thread 
    [self.progressView removeFromSuperView]; 
} 
0

你可以對視圖進行子類化,然後使用viewDidLoad函數來執行長時間運行的代碼。