请教在block中如何给实例变量赋值
1个回答
推荐于2016-11-09
展开全部
@interface AllBooksViewController ()
@end
@implementation AllBooksViewController
@synthesize bookList;
@synthesize searchResult;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = GREY_BACKGROUND;
[self sendHTTPRequest];
NSLog(@"# searchResult: %@", self.searchResult);
NSLog(@"# bookList: %@", self.bookList);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
// HTTPRequest
-(void) sendHTTPRequest {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[userDefaults stringForKey:@"username"], @"username", [userDefaults stringForKey:@"password"], @"password", EWB_SEARCH, @"function", nil];
[[SVHTTPClient sharedClient] setBasePath:BASEURL];
[[SVHTTPClient sharedClient] setSendParametersAsJSON:YES];
[[SVHTTPClient sharedClient] POST:@"/post/" parameters:dic
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([[response objectForKey:@"status"] integerValue] == 1) {
NSLog(@"# Response: %@", [response objectForKey:@"ewb_search_result"]);
// 出问题的地方,searchResult 在block内可以被付值,
self.searchResult = [response objectForKey:@"ewb_search_result"];
} else {
NSLog(@"The funtion ewb_search is failed!");
}
}];
}
@end
@end
@implementation AllBooksViewController
@synthesize bookList;
@synthesize searchResult;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = GREY_BACKGROUND;
[self sendHTTPRequest];
NSLog(@"# searchResult: %@", self.searchResult);
NSLog(@"# bookList: %@", self.bookList);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
// HTTPRequest
-(void) sendHTTPRequest {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[userDefaults stringForKey:@"username"], @"username", [userDefaults stringForKey:@"password"], @"password", EWB_SEARCH, @"function", nil];
[[SVHTTPClient sharedClient] setBasePath:BASEURL];
[[SVHTTPClient sharedClient] setSendParametersAsJSON:YES];
[[SVHTTPClient sharedClient] POST:@"/post/" parameters:dic
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([[response objectForKey:@"status"] integerValue] == 1) {
NSLog(@"# Response: %@", [response objectForKey:@"ewb_search_result"]);
// 出问题的地方,searchResult 在block内可以被付值,
self.searchResult = [response objectForKey:@"ewb_search_result"];
} else {
NSLog(@"The funtion ewb_search is failed!");
}
}];
}
@end
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询