Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDelegate class not found

I imported the AppDelegate.h in a lot of classes with:

#import "AppDelegate.h"

@interface LoginViewController : UIViewController<UITextFieldDelegate>
{

}

@property (nonatomic, retain) AppDelegate *app;

but somehow it stopped working in my loginviewcontroller.h. It says:

 unknown type name 'AppDelegate' [1]
 property with 'retain (or strong)' attribute must be of object type [3]

I made this class at the start and it always worked like it should. I didn't make any changes to the class or the AppDelegate when it starting with this error.

I can import it in other classes without problems. I also tried to recreate the class but it didn't help.

Anyone got any idea how to solve this weird error?

like image 505
Youri Avatar asked Dec 06 '22 16:12

Youri


1 Answers

It's not a good idea to use this line of code

@property (nonatomic, retain) AppDelegate *app;

in every class you need it. The simple way to access the delegate app where you need it is to do the following:

AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];

obviously you need to do:

#import "AppDelegate.h"

in the class where you use it.

If you want a cleaner way to do this, you can create a class method in your AppDelegate.h like the following:

+(AppDelegate*)sharedAppdelegate;

in AppDelegate.m is defined as follow:

+(AppDelegate*)sharedAppdelegate
{
    return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}

Then, where you need it you could just call (after importing AppDelegate.h):

AppDelegate* sharedApp = [AppDelegate sharedAppdelegate];

Hope it helps.

P.S. Why do you need to access the delegate?

like image 59
Lorenzo B Avatar answered Dec 29 '22 00:12

Lorenzo B