ios - 在其他类获取数据时停止 viewDidLoad 的其余部分

标签 ios objective-c cocoa-touch

在我的一个 View Controller 中,我根据从单独的 NSObject 类收到的“GET”数据设置标签。显然,设置标签比获取数据花费的时间要少得多,因此标签总是设置为 nil。我怎样才能确保在数据获取完成之前不设置标签。

这是在 NSObjectmyClass 中执行“getting”的方法

- (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"];;

}

然后在 View Controller 中设置标签:

- (void)viewDidLoad
{
[myClass getUserInfo:@"some_user"];

myLabel.txt = myClass.futureLabelStr;

}

请让我知道我是否需要添加更多内容或任何内容,我已尽我所能组织它,但我可能遗漏了一些内容。

最佳答案

你不想“停止”你的 viewController 的 viewDidLoad,你想通知它,当 信息发生变化。

您可以通过在 myClass 完成并调用 -userInfoResponse: 时发送通知(查看 NSNotificationCenter)或在 myClass 中实现委托(delegate)模式来实现。您可以将 viewController 设置为 myClass 的委托(delegate),并在 myClass 完成对 viewController 的提取后调用委托(delegate)方法,该方法本身会更新标签。

或者,查看您的代码,您可以将您的 viewController 设置为回调方法的接收者,只需对您的代码进行最小的更改,尽管这不是最佳方法,因为它违反了 MVC 模式:

[self doGetURL:getString callBackTarget:viewController callBackMethod:@"userInfoResponse:" failedMethod:@"connectionFailedDialog:"];

您当然需要在 myClass 中引用 viewController,并且 viewController 需要实现此方法(这是违反 MVC 模式的)。

关于ios - 在其他类获取数据时停止 viewDidLoad 的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138649/

相关文章:

ios - 为什么应用程序包并不总是一个包?

ios - 数据从 UITextField 传递到 HTML,在 iOS 中反之亦然

ios - 向 iOS 应用程序添加自定义字体以查找其真实姓名

ios - 如何迭代多个 NSDictionary 的键值之一,并将它们添加到 NSMutableArray?

ios - 单元格上的 UicollectionViewCell 选择更改边框颜色

iphone - 我正在从服务器获取很多名称,但我只想在 UITableView 中显示 2 个名称

ios - 如何创建一个自定义 UIView,根据 Objective-C 中的滚动方向从 UINavBar 后面向下或向上滑动?

cocoa-touch - Cocoa 和 Cocoa Touch 之间的主要区别是什么?

objective-c - 什么时候 if (!someObject)(someObject != nil 的缩写)不安全?

ios - 如何在 iOS 设备离线时加载本地 HTML 文件?