作为iOS编程新手,我有一个非常简单的问题。
我有an iPhone app,它获取Facebook用户数据(ID,姓名,性别,城市)。
使用NSLog
打印数据就好了:
id: 597287941
first_name: Alexander
last_name: Farber
gender: male
city: Bochum, Germany
但是我无法在我的UI中显示数据:
我尝试使用UITextField
和UILabel
。
我尝试过使用
[[self userId] setText:dict[@"id"]];
和
[_firstName setText:dict[@"first_name"]];
我已经连接了IBOutlets just fine(我认为):
以下是我的DetailViewController.h:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) NSDictionary *dict;
@property (weak, nonatomic) IBOutlet UILabel *userId;
@property (weak, nonatomic) IBOutlet UILabel *firstName;
@property (weak, nonatomic) IBOutlet UILabel *lastName;
@property (weak, nonatomic) IBOutlet UILabel *gender;
@property (weak, nonatomic) IBOutlet UILabel *city;
@end
DetailViewController.m中有问题的部分:
- (void)setDict:(NSDictionary *)dict
{
NSLog(@"id: %@", dict[@"id"]);
NSLog(@"first_name: %@", dict[@"first_name"]);
NSLog(@"last_name: %@", dict[@"last_name"]);
NSLog(@"gender: %@", dict[@"gender"]);
NSLog(@"city: %@", dict[@"location"][@"name"]);
[[self userId] setText:dict[@"id"]];
[_firstName setText:dict[@"first_name"]];
[_lastName setText:dict[@"last_name"]];
[_gender setText:dict[@"gender"]];
[_city setText:dict[@"location"][@"name"]];
}
答案 0 :(得分:1)
您正在初始化标签之前更新视图。仍然可以在setDict
方法中调用prepareForSegue
来传递信息,但将setText
调用移动到新视图控制器的viewDidLoad
方法中。这将在视图初始化后更新标签。
答案 1 :(得分:1)
上面的@DanielM是正确的,但这是他想说的。在segue上调用setDict方法。这没关系,但唯一的工作就是保持这些值直到准备好视图......
这是 DetailViewController的完整实现:
// DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
// this must save the values from Facebook until the VC is ready to use them
@property (strong, nonatomic) NSDictionary *dict;
@property (weak, nonatomic) IBOutlet UILabel *userId;
@property (weak, nonatomic) IBOutlet UILabel *firstName;
@property (weak, nonatomic) IBOutlet UILabel *lastName;
@property (weak, nonatomic) IBOutlet UILabel *gender;
@property (weak, nonatomic) IBOutlet UILabel *city;
@end
@implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
// this is the key: you cannot set the state of your views before they have
// been created.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self userId] setText:dict[@"id"]];
[_firstName setText:dict[@"first_name"]];
[_lastName setText:dict[@"last_name"]];
[_gender setText:dict[@"gender"]];
[_city setText:dict[@"location"][@"name"]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/* don't implement this. it's called too early to make a difference
- (void)setDict:(NSDictionary *)dict
{
NSLog(@"id: %@", dict[@"id"]);
NSLog(@"first_name: %@", dict[@"first_name"]);
NSLog(@"last_name: %@", dict[@"last_name"]);
NSLog(@"gender: %@", dict[@"gender"]);
NSLog(@"city: %@", dict[@"location"][@"name"]);
NSString *avatar = [NSString stringWithFormat:kAvatar, dict[@"id"]];
NSLog(@"avatar: %@", avatar);
[[self userId] setText:dict[@"id"]];
[_firstName setText:dict[@"first_name"]];
[_lastName setText:dict[@"last_name"]];
[_gender setText:dict[@"gender"]];
[_city setText:dict[@"location"][@"name"]];
}
*/
@end
答案 2 :(得分:0)
我认为setText已弃用SetText is Deprecated。我想你可以直接使用标签的文字属性。
firstName.text = dict[@"first_name"];
答案 3 :(得分:0)
我认为编译器不知道它是什么类。您可以使用下面的示例进行显式演员测试吗?
- (void)setDict:(NSDictionary *)dict
{
NSLog(@"id: %@", dict[@"id"]);
NSLog(@"first_name: %@", dict[@"first_name"]);
NSLog(@"last_name: %@", dict[@"last_name"]);
NSLog(@"gender: %@", dict[@"gender"]);
NSLog(@"city: %@", dict[@"location"][@"name"]);
[[self userId] setText:dict[@"id"]];
[_firstName setText:(NSString *)dict[@"first_name"]];
[_lastName setText:(NSString *)dict[@"last_name"]];
[_gender setText:(NSString *)dict[@"gender"]];
[_city setText:(NSString *)dict[@"location"][@"name"]];
}