iPhone - Issues accessing viewController variables from my delegate
Hi everybody that's my first post here, so i'll try to be as clear as possible.
So, in my project, I need to get an xml document from a webService, with SOAP, and then parse it in a NSMutableArray (This is done). Then I would like to display my table's content in a tableview. Problem is, in my delegate I can't access anything of my viewController.
I have 3 classes, SOAP_TestViewController, SOAP_TestAppDelegate and XMLParser. I'm sending the array containing the parsed file from XMLParser to the Appdelegate, and then I would like to update an array in my viewController to use it in order to up开发者_运维知识库date my tableview.
Here's a bit of code, may help to understand.
#import "SOAP_TestAppDelegate.h"
#import "SOAP_TestViewController.h"
#import "XMLParser.h"
@implementation SOAP_TestAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
NSLog(@"%@", viewController.donnees); //Here I can get what I want.
[window makeKeyAndVisible];
}
-(void)afficher{
NSLog(@"%@", viewController.donnees); //But there, it returns (null)...
[viewController reload];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
I spent like hours to find a solution. AND, I noticed something. Like the comments say in my code, I can access what I want in the applicationDidFinishLaunching
function, but not outside. So am I working on copies or something when i'm out of this function ? And what should I do to access my "donnees" table ?
I'm such a beginner in cocoa coding so please tell me if i'm doing things wrong.
(Please be indulgent with my English :-) I'm not bilingual yet ^^')
EDIT
My afficher
function looked like this at the beginning. I call it from XMLParser with the parsed array as parameter. I was sur it would work but...
-(void) afficher:(NSMutableArray *)array
{
NSLog(@"%@", array); //returns the array sent by XMLParser.
[viewController.donnees addObjectsFormArray:array];
NSLog(@"%@", viewController.donnees); //returns null.
}
That's weird, it's like donnees wasn't even allocated and initialized...
Are you sure your viewController is not nil? Ok, that's unlikely. Then are you sure your donnees array is not nil AND it is NSMutableArray, but not NSArray?
And, btw, you CAN access the properties out of appDelegate from anywhere. I'm not fond of this solution, but just to make a clarification on that. In any class you can do smth like:
#include "YourAppDelegate.h"
- (void)someMethod {
YourAppDelegate *yourDelegate = [[UIApplication sharedApplication] delegate];
yourDelegate.donnees = self.anotherArray;
}
Of course, donnees should be a property in this case. Cheers!
I would suggest setting a breakpoint that will stop the program any time the viewController
ivar is changed; to do this:
set a breakpoint in your appDelegate, then debug the program;
when the program stops in your appDelegate, execute this command in the gdb console:
(gdb) watch -location self->viewController
continue debugging.
Now, each time that viewController
is set a value, the program will stop, so you can inspect who is trying to set it to nil and why.
OLD:
The problem lies possibly with the step you describe as "I'm sending the array containing the parsed file from XMLParser to the Appdelegate".
Indeed, it seems that when your app starts, i.e., before the parsing, viewController.donnees
is defined; after the parsing, it is null. You are obviously doing something wrong in between.
It could be the parserDidEndDocument
method that tries to set the donnees
ivar in the wrong way. Without more code it is not possible to help further, but I hope that this hint could guide you in the right direction.
refer this:
-(void) rar:(NSMutableArray *)array
{
NSLog(@"%@", array); //returns the array sent by XMLParser.
[viewController.donnees addObjectsFormArray:array];
NSLog(@"%@", viewController.donnees); //returns null.
}
精彩评论