2013-06-04 118 views
0

我试图在应用程序启动后在模式视图中显示我的远程配置设置。一切正常,但视图更新其configs对象从它的NSURLConnection委托方法更新之前。如何在继续之前等待NSURLConnection委托完成?

我正在寻找一种解决方案,在尝试更新视图之前让委托方法完成。我宁愿不把这些功能放在委托方法本身中,所以我可以在其他情况下使用MYRemoteConfig

我怀疑这个解决方案是显而易见的,但是我已经很长时间没有考虑这个问题了。


viewDidAppear{}MYSettingsViewController.m

MYRemoteConfig* config = [[MYRemoteConfig alloc] init]; 
[configs updateSettings]; // I need these delegate methods to be done 
          // before the next line 
self.customerLabel.text = configs.customer; // Updates with empty box 
self.courseLabel.text = configs.course; 

-

updateSettings{}MYRemoteConfig.m

// code that gets uuid and sets up post request // 

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request 
                  delegate:self]; 
[connection start]; 
NSLog(@"Connection should have started."); 

然后在connectionDidFinishLoading{}:(数据附加到局部变量之后)

// pull JSON objects into dictionary 
[self updateProfile:settingsDictionary; 
NSLog(@"%@", settingsDictionary); //works 

updateProfile{}

// code that sets config attributes in singleton object // 

self.customer = [settings objectForKey:@"Customer"]; // I need this data 
self.course = [settings objectForKey:@"Course"];  // in my view controller 
+1

这已被回答http://stackoverflow.com/questions/16880686/different-nsstring-results-from-url-download –

+0

安装通知可能吗? ;-) NSNotificationCenter –

+0

或委托给你的'MYRemoteConfig'类。 – danypata

回答

1

你应该让MYSettingsViewController MYRemoteConfig控制器的代表,创造MYRemoteConfig一个委托协议,并要求你在connectionDidFinishLoading方法创建协议的方法。然后,在MYSettingsViewController中实现该方法将更新客户和课程标签。

+0

由于'connectionDidFinishLoading'将被多个对象调用,我应该让MYRemoteConfig拥有一个像'calledFromMYSVC'这样的属性,通过像initFromMYSVC这样的初始化设置吗?这是识别谁在调用函数的最好方法吗?因为这样做的目标是让MYRemoteConfig更新标签,但对其他类也是可重用的。 – user

+0

@user我不认为你需要做那样的事情。任何调用此类的类都将创建自己的MYRemoteConfig实例,并将自己设置为委托,因此不需要拥有一个属性来跟踪委派的人员。 – rdelmar

相关问题