tags:

views:

821

answers:

3

I want to make a program that parses an XML file and then updates labels on 2 different tab bar views. On both of these views, I have a refresh button to update the results. What I'd like to do is update both of the views labels from either view. I figure the AppDelegate is probably a good choice to make this happen, but I tried creating classes that the AppDelegate can access but since they're instances of a class they contain no values. I get no errors but the labels don't update even though the data changes. This is the method in my AppDelegate that is called after the XML is parsed:

-(void)callUpdateValues {
    NSLog(@"Calling Update from AppDelegate");
    home *homeController;
    data *dataController;
    [homeController updateValues];
    [dataController updateValues];
    }

One of the update methods looks like:

- (void)updateValues {
NSLog(@"Call Home");
[label1 setText: [[[[totalData objectAtIndex:0] objectForKey:@"nodeChildArray"] objectAtIndex:7] valueForKey:@"nodeContent"]];
[label2 setText:[[[[totalData objectAtIndex:0] objectForKey:@"nodeChildArray"] objectAtIndex:1] valueForKey:@"nodeContent"]];
}

So the view calls the AppDelegate method "callUpdateValues" and then that method should call the individual "updateValues" methods in each view. I am not an expert on this by any means, and I'm really just trying to get an idea of how programming on the iPhone works. I'm probably just not understanding something here, and if someone could give me some sort of answer I'd appreciate it.

A: 

There's some really great stuff getting done by Apple for XML on the iPhone: XML Reading Material

The first snippet is out of place. I think what you're missing is that you need to create your instances within the AppDelegate.h, expose them using properties (and synthesizing them in the .m). Then you're update structure should fit better.

If you're just picking up iPhone programming, start digging into the guides that apple provides, and even if you're not into that, start pulling down at least 5 sample code projects a day. The beauty of them is that you can build them (even onto your iphone) and if you like a feature, you can see how it's done. Alternatively, get the grapefruit book from APRESS. Beginning iPhone. Hope this helped.

TahoeWolverine
A: 

In the example you gave, homeController and dataController are not properly initialized. If I understand your project correctly, you would have created instances of the homeController and dataController classes in your main XIB file, and connected them up to the appropriate views (label1 and label2). Your AppDelegate should, then, look something like this:

...

@class homeController;
@class dataController;

@interface AppDelegate
{
    IBOutlet homeController * home;
    IBOutlet dataController * data;
}

...

@end

With this in place, you would add (in your application XIB file), links from your homeController and dataController instances to the appropriate outlets (labeled home and data) in your application delegate.

Then, you could simply reference them by name in your callUpdateValues method:

-(void)callUpdateValues {
    NSLog(@"Calling Update from AppDelegate");
    [home updateValues];
    [data updateValues];
}

On a side note, Cocoa coding standards usually specify that class names are capitalized. This is, of course, up to your personal taste, but if you're just getting started in Cocoa, it may be worth drinking one more cup of kool-aid at this point, just so your code will "fit in" with what most other developers are doing. Again, totally up to you!

e.James
+1  A: 

Cocoa has a number of classes available for notifying interested parties of changes. Directly calling methods as you describe makes things much more closely coupled than you need to.

In your method that generates the update you'd have:

[[NSNotificationCenter defaultCenter] postNotificationName:@"IGotSomeNewData"
                                                    object:newData
                                                  userInfo:nil];

And in the classes that want to hear about updates you'd register for the notification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(newStuff:)
                                             name:@"IGotSomeNewData" object:nil];

And then implement the method that gets called when something happens:

- (void) newStuff: (NSNotification *)notification {
  id newData = [notification object];
  // Do stuff
}
Stephen Darlington
Oh my, I cant thank you enough, completely solved a massive problem I was having. THANKS!
Jessica