2015-06-22 26 views
-1

在我的應用程序中,我初始化一個新對象,其中有方法調用NSURLConnection的方法sendAsynchronousRequest。請求後,我想在調用者UIViewController中調用一個方法。我試圖使用靜態方法,但我然後我無法控制IBOutlet s。我怎樣才能做到這一點?異步請求後的調用方法obj -c

我曾嘗試以下:

// First test  
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if(data.length > 0) 
    { 
     NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     self.result = responseBody; 
     PhotoViewController *photo = [[PhotoViewController alloc] init]; 
     [photo finishedPost:self]; // Doesnt work 
    } 
}]; 

// Second test 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if(data.length > 0) 
    { 
     NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     self.result = responseBody; 
     [PhotoViewController finishedPost:self]; // Doesnt work 
    } 
}]; 
+1

需要的代碼。 –

+0

http://pastebin.com/SuH3yGPS –

+0

您是否嘗試過在'completion'塊中使用斷點? 'photo'對象是新的引用(因爲你在塊中分配它),如果你想更新'PhotoViewController'的任何現有對象,你必須檢索它或保存在類中。正如格洛芬德爾回答的那樣。 –

回答

2

你必須「記住」這UIViewController調用對象。這可以通過屬性來實現。

在.H

@property (nonatomic) UIViewController *viewController;

在.m文件

@synthesize viewController;

之前調用方法,設置與

anObject.viewController = self; 

屬性然後,你」我們可以撥打

[viewController finishedPost:self]; 

內部的sendAsynchronousRequest:方法的完成處理程序。

+0

謝謝你的工作。 –