views:

201

answers:

3

Hello, I've been having with an objective-c class which appears to be losing its pointer reference between methods of the same class.

In the MyTableViewController.h file, I declare:

@interface SettingsTableViewController : UITableViewController <UITextFieldDelegate>{
    OCRAppDelegate *delegate;
}

MyTableViewController.m file

- (id) init {
    self = [ super initWithStyle: UITableViewStyleGrouped ];
    delegate = [(OCRAppDelegate *)[[UIApplication sharedApplication] delegate] retain];
}

The problem is when the "MyTableViewController" view appears again and a different method is executed within that same class, the delegate pointer (which was assigned during the init method) is no longer there. I tried to retain, but to no avail.

Would anyone know why this is, it seems like perhaps it is a fundamental Objective-C issue which I am missing.

Appreciate your help.

Thanks, Winston

A: 

When you say it's no longer there, what do you mean? What has its value changed to?

If the value is 0x0 (i.e. nil) either something changed it to nil or it always was nil. Have you checked that it is not nil when you assign it? Perhaps with an NSLog statement.

I suspect that, since it works in the viewWillAppear method, but not in the init method, the app delegate has not been initialized at the point init method is called so you are actually setting it to nil.

invariant
Hello invariant,The value changes to 0x0.However, when I add the "delegate = [......]" line to the viewWillAppear method- (void) viewWillAppear:(BOOL)animated{ [ super viewWillAppear:animated]; delegate = (OCRAppDelegate *)[[UIApplication sharedApplication] delegate];}It works. Thank you so much for your quick response!Winston
Winston
Hi, OK I edited the answer based on that.
invariant
I can confirm that when I debug using XCode and go to the - (id) init function, the delegate pointer is set correctly. However, upon reentering another function in the .m class, its lost. Its almost as if when UIViews are changed, any pointer reference gets lost.In my application delegate, the only thing I do is: NSString *path=[[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; self.settings = dict; [dict release]; OCRsettings = [settings objectForKey:@"OCR"];
Winston
+1  A: 

The fact that the init method doesn't return anything might be the root of the problem - it has to return 'self'.

- (id) init {
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        delegate = [(OCRAppDelegate *)[[UIApplication sharedApplication] delegate] retain];
    }
    return self;
}
dbarker
Hi dbarker,Thank you for your response. Actually, I apologize, but I put in a code snippet and left the "return self" out.Anyway, it appears that I do have the answer below which looks like it fixed the problem.Thanks,Winston
Winston
A: 

I think I may have found the answer.

For any initializations for the class, it should be done in ViewDidLoad. Prior to this, I believe that in the init method, the class is has not been fully set up.

What is interesting is for all my other views, the ViewDidLoad is called when the view is about to be loaded so all my variables/pointers exist. However, in the one class above, the viewdidload method is called very early on, and the delegate pointer back to the app delegate has not been fully set up.

Winston