2012-02-20 32 views
0

我一直在想着我遇到的一個依賴。我是Objective-C中的新成員,所以請對我輕鬆一點。我甚至不知道如何谷歌我遇到的問題。三類依賴 - 返回數據

我有三類:

  1. RootViewController
  2. HttpRequestWrapper
  3. ManagementClass

RootViewController繼承ManagementClass。 (@interface RootViewController : ManagementClass <UINavigationControllerDelegate>)。

所以在ManagementClass我調用這個函數:

[[self.navigationController topViewController] getTableData]; 

其中topViewControllerRootViewController(但後來我想改變它,此刻任何topViewController)。

此函數調用getTableDataHttpRequestWrapperURLConnection被稱爲其所有代表。但是,當涉及到委託方法 - (void)connectionDidFinishLoading:(NSURLConnection *)connection我希望NSNotification通知RootViewController加載請求中的數據已完成並使用數據填充表。但NSNotification不會通知RootViewController,即使它是導航堆棧上的topViewController

所以我的問題是我怎樣才能從URLConnection返回到RootViewController的數據,即使請求通過ManagementClass以某種方式初始化。

這裏是我的問題的代碼位:在.m文件中的.h文件

@interface ManagementClass : UITableViewController {} 

RootViewController

#import "RootViewController.h" 
...... 

// Get the data using the class HttpRequstWrapper (where I wrap the request in NSURLConnection) 
- (void) getTableData{ 
    httpRequestWrapper = [HttpRequestWrapper alloc]; 
    [httpRequestWrapper getXMLDataWithURL:[NSString stringWithFormat:@"equipment/xml"]]; 
} 
- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // Get Table Data 
    [self getTableData]; 
} 

- (void) populateTableData{ 

    // Maybe change this line that error is shown if no data is found 
      if (httpRequestWrapper.dataFromTheHttpRequest == nil){ 
     NSLog(@"Data got from the HttpRequestWrapper is nil or empty"); 
    } 

    // Parse the returned data 
    xmlcont = [[XMLController alloc] loadXMLByURL:httpRequestWrapper.dataFromTheHttpRequest]; 

    // Get the names of the Equipments as names of each Section 
    arrayEquipments = [NSMutableArray alloc]; 
    arrayEquipments = xmlcont.equipments; 
    sectionsTitles = [[NSMutableArray alloc]init]; 
    for (Equipment *eq in arrayEquipments){ 
     [sectionsTitles addObject: eq.equipmentName]; 
    } 

    // Reload the data in TableView 
    [self.tableView reloadData]; 
} 

- (void) viewDidLoad{ 
    .... 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateTableData) name:@"connectionFinishedLoading" object:nil]; 

} 

ManagementClass

@implementation ManagementClass 

- (void) refreshPage { 
    if ([[self.navigationController topViewController] isKindOfClass: [RootViewController class]]){ 
     [[self.navigationController topViewController] getTableData]; 
    } 
} 

而在HttpRequestWrapper I類有:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
...... 
     if ([urlExtension rangeOfString: @"notification"].location != NSNotFound) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoading1" object:nil]; 
     } else if ([urlExtension rangeOfString: @"execution/workflow"].location != NSNotFound) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoadingPhaseView" object:nil]; execution/workflow 

     } else if ([urlExtension rangeOfString: @"equipment/xml"].location != NSNotFound) { 
     //[[self.navigationController topViewController] populateTableData]; - not working 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoading" object:nil]; 
    } 
....... 
} 

我想一個方法,使控制器可重複使用成爲可能。 我有一個想法[[self.navigationController topViewController] populateTableData]connectionDidFinishLoading但導航控制器不能從HttpRequstWrapper調用,它只是不會執行。我仍然無法弄清楚爲什麼。我只能使用ViewViewController執行navigationController方法,該方法目前可見。

回答

0

從我所看到的,你聽在RootViewController的「connectionFinishedLoading」和你在HttpRequestWrapper發佈「connectionFinishedLoading1」。他們必須是相同的才能被RootViewController捕獲

+0

對不起,我沒有正確複製粘貼if命令的最後部分。我的錯。我現在編輯了所有的Ifs。代碼的整個目標是,一旦呈現視圖,我下載表格,如果服務器上有更改,下一次我想刷新表格數據。所以我第二次從Management類調用getTableData,但是當數據完成加載並重新加載數據時,我需要通知RootViewController。 – Lily 2012-02-20 12:52:51

+0

我設法調用[[self.navigationController topViewController] populateTableData];在httpRequestWrapper類中使用此方法:MyAppDelegate * appDel =(MyAppDelegate *)[[UIApplication sharedApplication] delegate];然後 [[appDel.navigationController topViewController] populateTableData];我認爲這不是最好的解決方案,但它工作得很好。 – Lily 2012-02-20 14:08:51