我有一个命令应该在特定的时间密集代码执行之前加载子视图。但是,命令运行,及时执行代码,然后显示子视图。我能解决这个问题吗?
progressViewController = [[ProgressView alloc] initWithNibName:@"ProgressView" bundle:[NSBundle mainBundle]];
[self.view addSubview:[progressViewController view]];
NSString *name=@"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 :(得分: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];
}
答案 1 :(得分:0)
您可以对视图进行子类化,然后使用viewDidLoad函数执行长时间运行的代码。