三类依赖 - 返回数据

时间:2012-02-20 12:31:21

标签: objective-c ios notifications uitableview urlconnection

我一直想着我遇到的一种依赖。我是Objective-C的新手,所以请放轻松我。我甚至不知道如何谷歌我遇到的问题。

我有三个班级:

  1. RootViewController
  2. HttpRequestWrapper
  3. ManagementClass
  4. RootViewController继承ManagementClass的位置。 (@interface RootViewController : ManagementClass <UINavigationControllerDelegate>)。

    所以在ManagementClass我称这个函数为:

    [[self.navigationController topViewController] getTableData]; 
    

    其中topViewControllerRootViewController(但稍后我想为任何topViewController更改它。)

    此功能getTableData调用HttpRequestWrapper并在其URLConnection中调用其所有代理。但是当谈到委托方法时,(void)connectionDidFinishLoading:(NSURLConnection *)connection我希望NSNotification通知RootViewController从请求中加载数据并使用数据填充表。但是NSNotification即使在导航堆栈上是RootViewController,也不会通知topViewController

    所以我的问题是如何将数据从URLConnection恢复回RootViewController,即使请求是通过ManagementClass以某种方式初始化的。

    以下是我的问题的一些代码:

    班级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: 在.h文件中

    @interface ManagementClass : UITableViewController {}
    
    <。>文件中的

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

    HttpRequestWrapper课程中我有:

    - (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调用,它根本就不执行。我仍然无法弄清楚为什么。我只能使用目前可见的navigationController的{​​{1}}执行ViewController方法。

1 个答案:

答案 0 :(得分:0)

从我看到的,你正在RootViewController中监听“connectionFinishedLoading”,你在HttpRequestWrapper中发布了“connectionFinishedLoading1”。它们必须与RootViewController

相同