2013-06-04 115 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

相關問題