Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you access application delegate from within a UITableViewDataSource function?

I'm trying to figure out the SQLite functionality for the iPhone, and I'm having some problems reading my database file from an overridden UITableViewDataSource function. I am storing the database file location in an ivar (as an NSString) in my application delegate, however when I try to access that string from an overridden UITableViewDataSource function, it returns some other object. If I access the string from any of the classes own instance methods, it works fine.

Is there any way to access the application delegate from within overridden UITableViewDataSource functions?

like image 942
jammur Avatar asked May 03 '09 14:05

jammur


2 Answers

The Application is a singleton which maintains a reference to the app delegate. You can always access your app delegate using:

[UIApplication sharedApplication].delegate

You may need to cast the return to your own app delegate class to get rid of warnings. Even better, write an accessor that returns your upcast app delegate:

#pragma mark access to app delegate etc.
+ (MyAppDelegateClass*) sharedAppDelegate; {
    return (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate];
}
like image 105
Rog Avatar answered Oct 26 '22 04:10

Rog


Roger is correct, but I personally find it extremely confusing to mix dot syntax and bracket syntax in the same statement.

If this confuses you as well, the equivalent syntax, using only bracket notation, is:

[[UIApplication sharedApplication] delegate];

and yes, you may need to cast the result to be your application delegate class, rather than the generic UIApplicationDelegate class, or you will get a number of compiler warnings, most likely.

like image 22
mmc Avatar answered Oct 26 '22 04:10

mmc