在我的一个视图控制器中,我根据从单独的NSObject
类接收的“GET”数据设置标签。显然,设置标签所需的时间要少得多,然后才能获取数据,因此标签始终设置为nil。在获取数据之前,如何确保标签未设置。
这是在NSObject
班myClass
- (void) doGetURL:(NSString *) urlstring
callBackTarget:(id) target
callBackMethod:(NSString *) method
failedMethod:(NSString *) method_failed
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlstring]];
NSLog(@"-- get URL with cookie : [%@] and hash:(%@)", [self cookie], [self modhash]);
if (cookie && [cookie length] > 0)
{
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
cookieDomain, NSHTTPCookieDomain,
@"/", NSHTTPCookiePath,
@"reddit_session", NSHTTPCookieName,
cookie, NSHTTPCookieValue,
// [cookie stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], NSHTTPCookieValue,
nil];
NSHTTPCookie *http_cookie = [NSHTTPCookie cookieWithProperties:properties];
NSArray* cookies = [NSArray arrayWithObjects: http_cookie, nil];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers];
}
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSString *connectionKey = [NSString stringWithFormat: @"%ld", ((intptr_t) connection)];
NSMutableDictionary *dl = [[NSMutableDictionary alloc] init];
[dl setValue:connectionKey forKey:@"connectionKey"];
if (target && method)
{
[dl setValue:target forKey:@"afterCompleteTarget"];
[dl setValue:method forKey:@"afterCompleteAction"];
}
[dl setValue:method_failed forKey:@"failedNotifyAction"];
[connections setValue:dl forKey:connectionKey];
}
这是在myClass
- (void)getUserInfo:(NSString*)user
{
NSString *getString = [NSString stringWithFormat:@"%@/user/%@/about.json",server,user];
[self doGetURL:getString callBackTarget:self callBackMethod:@"userInfoResponse:" failedMethod:@"connectionFailedDialog:"];
}
回拨方法:
- (void)userInfoResponse:(id)sender
{
NSLog(@"userInfoResponse in()");
NSData * data = (NSData *) sender;
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSDictionary *response = [json objectForKey:@"data"];
//futureLabelStr is a property of myClass
futureLabelStr = [response objectForKey:@"name"];;
}
然后在视图控制器中设置标签:
- (void)viewDidLoad
{
[myClass getUserInfo:@"some_user"];
myLabel.txt = myClass.futureLabelStr;
}
请让我知道我需要添加更多或任何我尽力组织它的东西,但我可能错过了一些东西。
答案 0 :(得分:3)
你不想“停止”你的viewController的viewDidLoad,你想要通知它,当 信息发生了变化。
您可以通过在myClass完成时发送通知并调用-userInfoResponse:
(查看NSNotificationCenter)或在myClass中实现委托模式来实现。您可以将viewController设置为myClass的委托,并在myClass完成读取本身更新标签的viewController时调用委托方法。
或者,查看代码,您可以将viewController设置为回调方法的接收者,只需对代码进行最少的更改,即使不是最佳方法,因为它违反了MVC模式:
[self doGetURL:getString callBackTarget:viewController callBackMethod:@"userInfoResponse:" failedMethod:@"connectionFailedDialog:"];
您当然需要在myClass中引用viewController,而viewController需要实现此方法(这是一种MVC模式违规)。
答案 1 :(得分:0)
在新线程上发送数据调用并正常完成viewDidLoad。然后使用NSNotification中心从谁获取此(应该是模型)到viewController说“嘿,我得到了你的标签,来得到它并刷新”
然后VC将使用模型中的数据设置标签。请查看此链接以使用NSNotificationCenter stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c。
对于多线程读取大中央调度。