请教在block中如何给实例变量赋值
1个回答
展开全部
@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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询