2011-03-05 54 views
1

我有一个名为'LoginViewController'的文件,需要从'RootViewController.m'文件中执行一个动作。我做了一个NSLog,它的功能,但实际的脚本不。来自不同班级的IBAction无法正常工作?

我也登录了我的LoginViewController中的#import "RootViewController.h"

LoginViewController

RootViewController *test = [[RootViewController alloc] init]; 
[NSThread detachNewThreadSelector:@selector(updateAlbumsAfterLogin) toTarget:test withObject:nil]; 

RootViewController的

- (void)updateAlbumsAfterLogin { 

NSLog(@"Updating albums after login!"); 

// Set up loading box and show it 
UIImage *imageLoop = [UIImage imageNamed:@"loading_box.png"]; 
imagefour = [[UIImageView alloc] initWithImage:imageLoop]; 
imagefour.frame = CGRectMake(100, 80, 116, 116); 
[self.view addSubview:imagefour]; 

// Set up loading text and show it 
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(97, 60, 130, 100)]; 
myLabel.text = @"Loading..."; 
myLabel.textColor = [UIColor whiteColor]; 
myLabel.textAlignment = UITextAlignmentCenter; 
myLabel.backgroundColor = [UIColor clearColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica" size: 16.0]; 
myLabel.numberOfLines = 0; 
//[myLabel sizeToFit]; 
[self.view addSubview:myLabel]; 

// Set up spinner and show it 
myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
myIndicator.center = CGPointMake(158, 155); 
myIndicator.hidesWhenStopped = YES; 
[self.view addSubview:myIndicator]; 
[myIndicator startAnimating]; 

[NSThread detachNewThreadSelector:@selector(updateAlbums) toTarget:self withObject:nil]; 
//myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector: @selector(updateAlbums) userInfo: nil repeats: YES]; 
} 

在此先感谢, 库尔顿。

如果您需要任何其他代码,请随时索要!

回答

0

您正在分离一个新线程来完成这项工作并更新您的视图,但只允许主线程更新视图。这就是为什么你可以看到你的NSLog()消息发生,但你的UI更新没有。

处理这个问题的标准方法是让主线程分离一个新线程(或者在后台线程上执行选择器),然后一旦完成了取回和数据处理,就在使用数据的主线程上执行选择器你获取更新UI。

+0

嗨,谢谢你的回复。我到底需要在每个文件中进行更改才能做到这一点?谢谢! – iosfreak 2011-03-05 03:47:25

+0

我在这个其他StackOverflow线程中描述了类似的东西:http://stackoverflow.com/questions/5188725/array-out-of-bounds-and-slow-slow-app/5189025#5189025 – 2011-03-05 04:16:22

+0

感谢您的链接,但是当实施它,它不适用。尽管顶部的活动指标停止,但内容不会刷新并且表格视图为空白。 – iosfreak 2011-03-05 04:44:03