Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Notify third ViewController that data has been loaded

Lily.PLily.P Posts: 12Registered Users
I've been thinking for over a day regarding one dependence I came a cross. I am new in Objective-C, so please take it easy on me. I didn't know even how to google the problem I came across.

I have three classes: * RootViewController * HttpRequestWrapper * ManagementClass Where RootViewController inherits ManagementClass. (@interface RootViewController : ManagementClass ).

So in ManagementClass I call this function:

[[self.navigationController topViewController] getTableData]; 

where the topViewController is the RootViewController (but later I want to change it for any topViewControlle at the moment).

[/HTML][/HTML]This function getTableData calls HttpRequestWrapper and in it URLConnection is called with its all delegates. But when it comes to the delegate method - (void)connectionDidFinishLoading:(NSURLConnection *)connection I want with NSNotification to notify the RootViewController that loading the data from the request is done and to populate the table with the data. But the NSNotification does not notify the RootViewController even tough it is a topViewController on the navigation stack.

So my question is how can I get the data back from the URLConnection to the RootViewController, even tough the request was initialized via the ManagementClass in a way.

Here is bit of code of my problem:

The class 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];

}

The class ManagementClass: in the .h file

@interface ManagementClass : UITableViewController {}

in the .m file

@implementation ManagementClass

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

And in the httpRequestWrapper class I have:

- (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];
.......
}

I want in a way to make Controllers reusable as possible. I had an idea of putting [[self.navigationController topViewController] populateTableData] in connectionDidFinishLoading but the navigation Controller can't be called from the httpRequstWrapper, it simply does not executes. I still can't figure out why. I can only execute the navigationController methods whiten the ViewControrller of the View that is visible at the moment.
Post edited by Lily.P on

Replies

  • smithdale87smithdale87 Posts: 4,286iPhone Dev SDK Supporter
    I see that you are posting a notificiation when connection finished, but I don't see anywhere that you registered to observe the notification in your RootViewController? So basically your notif is being called, but no one is listening..
  • Lily.PLily.P Posts: 12Registered Users
    smithdale87;410718 said:
    I see that you are posting a notificiation when connection finished, but I don't see anywhere that you registered to observe the notification in your RootViewController? So basically your notif is being called, but no one is listening..

    There is and observer in viewDidLoad, just scroll bit the code.
    - (void) viewDidLoad{
    ....
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateTableData) name:@"connectionFinishedLoading" object:nil];

    }
Sign In or Register to comment.