我试图做一些我认为很容易做的事情,但由于某些原因(缺乏经验),它不起作用。我从JSON响应中提取一些值,并将视图控制器上的标签设置为变量。当我执行并调试它时,值是存在的,但是由于某些原因,我不能将任何标签设置为它们,或者至少它们没有出现。HEre是我的视图控制器的代码。
#import "VideoDetailViewController.h"
#import "VideoListViewController.h"
#import "MapAnnotation.h"
#import "VideoDetail.h"
@interface VideoDetailViewController ()
@property (readonly) NSString *api_username;
@property (readonly) NSString *api_password;
@property (readonly) NSString *nplays;
@end
@implementation VideoDetailViewController
@synthesize Plays;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = self.name.name;
// Load the data on a background queue...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"https://%s:%s@api.wistia.com/v1/stats/medias/c3e4797d8f.json", "api", "1b75e458de33a9b3f99d33f6bf409a7e145c570a"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"%@", [jsonDict objectForKey:@"play_count"]);
NSNumber *_plays = [jsonDict objectForKey:@"play_count"];
_nplays = [NSNumberFormatter localizedStringFromNumber:_plays numberStyle:NSNumberFormatterNoStyle];
// SET LABEL TO VALUE
Plays.text = _nplays;
}
);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end编辑
我再次正确地设置了IBOutlet,标签文本仍然没有改变。甚至我设置的NSLog检查都说标签文本是我要设置的数字,但它在标签中没有变化。
// NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"%@", [jsonDict objectForKey:@"play_count"]);
NSNumber *_plays = [jsonDict objectForKey:@"play_count"];
_nplays = [NSNumberFormatter localizedStringFromNumber:_plays numberStyle:NSNumberFormatterNoStyle];
self.textEngagement.text = _nplays;
NSLog(@"%@", self.textEngagement.text);发布于 2014-02-06 15:56:35
您正在尝试在后台线程上设置标签文本。不应尝试在后台线程上更新UI。在更新标签值之前,您需要返回到主线程:
dispatch_async(dispatch_get_main_queue(), ^{
self.textEngagement.text = _nplays;
});发布于 2014-02-05 22:17:14
您需要为您的标签设置一个IBOutlet。最简单的方法是在IB中打开您的界面,然后打开一个辅助编辑器并将其设置为automatic。它可能显示视图控制器的.m文件或.h文件。切换到.h文件,如果辅助编辑器没有自动带您到那里。然后用其他属性声明从标签拖动到标头区域。这将使您可以创建一个新属性。IB (Interface )将在头中添加正确的代码,并连接出口,这样一旦视图控制器的视图加载,它就处于活动状态。
在您的示例中,您将设置属性"play“(按照目标C命名约定,它应该是小写"p",而不是"Play")。
发布于 2014-02-05 22:42:54
要么在标头接口中添加IBOutlet UILabel声明
@interface VideoDetailViewController ()
{
IBOutlet UILabel *title;
}
@property (readonly) NSString *api_username;
@property (readonly) NSString *api_password;
@property (readonly) NSString *nplays;
@end然后将title对象连接到在Interface中创建的标签上
或
在IB (查看部分,下面模式)的标签属性中设置标签(即11)和
UILabel *name = (UILabel*)[self.view viewWithTag:11];
[name setText:self.name.name];https://stackoverflow.com/questions/21589569
复制相似问题