我想显示用户信息。我正在尝试使用 AFNetworking AFHttpClient 和块来做到这一点。这是我的代码。你知道我能做什么吗?我这样做是为了在其他视图中完成一个 tableView 并且它可以工作。但是在这里我不能显示一个带有用户名的简单 UILabel
#import "YPProfileViewController.h"
#import "YPNetworkManager.h"
#import "Developer+Helper.h"
@interface YPProfileViewController (){
SelectionSuccessBlock2 successBlock;
}
@end
@implementation YPProfileViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *userNameLbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 40, 100, 20)];
NSString *userNameStr = [NSString stringWithFormat:@"Name "];
[userNameLbl setText:userNameStr];
[self.view addSubview:userNameLbl];
[self loadUserInfoFromService];
}
在这里我使用一个块
-(void)loadUserInfoFromService{
__weak typeof(self) weakSelf = self;
successBlock = ^(NSDictionary *newDev) {
NSLog(@"data ");
if (newDev){
[weakSelf showData:newDev];
}
};
[ypNetManager getUserInformation:successBlock error:NULL];
}
我在这里显示标签:
-(void)showData:(NSDictionary *) devInfo{
Developer *dev = [Developer createDevelopertWithDictionary:devInfo];
UILabel *userNameLbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 40, 100, 20)];
NSString *userNameStr = [NSString stringWithFormat:@"Name : %@", dev.user_name];
[userNameLbl setText:userNameStr];
[self.view addSubview:userNameLbl];
}
@end
这是我在 AFNetworking AFHttpClient 客户端中使用的方法:
-(void)getUserInformation:(SelectionSuccessBlock2)success error:(SelectionErrorBlock)error {
NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:kAPIProjectList parameters:nil];
[request setTimeoutInterval:kTimeOutRequest];
AFJSONRequestOperation *requestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"data");
NSLog(@"%@ Susscess JSNON Response: %@", NSStringFromSelector(_cmd),JSON);
NSDictionary *devInfo = [[NSDictionary alloc] init ];
devInfo = [JSON valueForKey:kTagUser];
NSLog(@"data %@", devInfo);
if (success) {
success(devInfo);
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *aError, id JSON) {
NSLog(@"%@ Failure JSNON Error%@", NSStringFromSelector(_cmd), aError);
if (error) {
error(aError);
}
}];
}