从背景线程中的委托方法更改UILabel的文本不起作用 [英] Change text of UILabel from Delegate Method in Background Thread Not Working
问题描述
HomeViewController.h
HomeViewController.h
#import "DataParser.h"
@interface HomeViewController : UIViewController <DataParserDelegate>
{
UILabel *theLabel;
}
HomeViewController.m
HomeViewController.m
#import "HomeViewController.h"
@implementation HomeViewController
-(void)viewDidLoad
{
theLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 100, 30)];
theLabel.text = @"Bob";
theLabel.font = [UIFont italicSystemFontOfSize:20.0];
theLabel.textColor = [UIColor whiteColor];
theLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:theLabel];
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[theButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[theButton setTitle:@"Big Button" forState:UIControlStateNormal];
theButton.frame = CGRectMake(100,100,200,50);
[self.view addSubview:theButton];
}
-(void)clicked:(id)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),
^{
DataParser *data = [[DataParser alloc] init];
data.delegate = self;
[data loadSomething];
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"Thread done");
});
});
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
theLabel.frame = CGRectMake(250,250,100,30);
theLabel.text = @"This message displays fine";
}
-(void)loadMessage:(NSString *)message
{
NSLog(@"Received message: %@", message);
theLabel.frame = CGRectMake(300,300,100,30);
theLabel.text = @"This message won't display";
}
@end
DataParser.h
DataParser.h
@protocol DataParserDelegate;
@interface DataParser : NSObject
@property (weak, nonatomic) id <DataParserDelegate> delegate;
-(void)loadSomething;
@end
@protocol DataParserDelegate <NSObject>
-(void)loadMessage:(NSString *)message;
@end
DataParser.m
DataParser.m
#import "DataParser.h"
@implementation DataParser
@synthesize delegate=_delegate;
-(void)loadSomething
{
[[self delegate] loadMessage:@"ASDF"];
}
@end
- = - = - = - = - = - = - = - = - = - = - = - = - = -
-=-=-=-=-=-=-=-=-=-=-=-=-=-
基本上,我创建一个标签并将其添加到屏幕上。此时,您可以清楚地看到一个带有白色文字的蓝色按钮,上面写着Bob。旋转屏幕时,按钮可以更改文本和位置。但是,如果单击该按钮,它将创建一个dataParser对象,将委托设置为self,并调用loadSomething。在dataParser对象中,loadSomething只调用委托方法loadMessage。
Basically, I create a label and add it to the screen. At this point you can clearly see a Blue button with white text saying "Bob" on it. When you rotate the screen, the button changes text and position just fine. However, if I click the button, it creates a dataParser object, sets the delegate to self, and calls "loadSomething". Within the dataParser object, "loadSomething" merely calls the delegate method "loadMessage".
问题是,当调用loadMessage时,NSLog语句打印正确(收到消息:ASDF),但是,标签不会移动,也不会更改其文本。为什么是这样?我该如何解决这个问题呢?
The question is, when "loadMessage" gets called, the NSLog statement prints correctly ("Received message: ASDF"), however, theLabel doesn't move and doesn't change its text. Why is this? And how do I fix this issue?
推荐答案
试试这段代码:
-(void)loadMessage:(NSString *)message
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Received message: %@", message);
theLabel.frame = CGRectMake(300,300,100,30);
theLabel.text = @"This message displays great!";
});
}
它应该在主线程上运行。
It should run it on the main thread.
这篇关于从背景线程中的委托方法更改UILabel的文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!