AppDelegate class not found - ios

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?

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?

Declare a forward reference in .h file
#class AppDelegate
#interface LoginViewController : UIViewController<UITextFieldDelegate>
{
}
// keep it as assign rather than retain to keep retainCount leveled for the variable
#property (nonatomic, assign) AppDelegate *app;
in .m file, grab the pointer to Appdelegate by importing AppDelegate.h and then assigning the variable
#import "AppDelegate.h"
- (void)viewDidLoad
{
self.app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//use the variable.
}

Related

Accessing AppDelegate Property

I would like to pass a string to all my other view controllers from the AppDelegate. I have defined a variable called appName and assign it a string
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSString *appName;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize appName;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
appName=#"SatKacPara";
return YES;
}
The following implementation does not have access to appName property. I could not able to figure out.
ViewController1.m
appLabel.text=[(AppDelegate*) [UIApplication shareApplication].delegate ].appName;
You have a simple typo in your line of code. It should be:
appLabel.text = ((AppDelegate*)[UIApplication sharedApplication].delegate).appName;
But using such a property isn't necessary. You can get the app's display name without hardcoding it.
Get rid of your appName property and change this line of code to:
appLabel.text = [NSBundle mainBundle].infoDictionary[#"CFBundleDisplayName"];
This has the advantage of showing the right name even if you rename your app or localize it.

How to use int of app delegate in other class in IOS?

Can someone help me in answer below 3 question? It will help me to solve my problem.
How to declare int in app delegate.
How to transfer this int value to other class.
How to NSLog this value in other class.
// AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic) int myIntVariable;
#end
// ViewController.m
#import "AppDelegate.h"
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
delegate.myIntVariable = 4;
NSLog(#"%d", delegate.myIntVariable);
}
Declare int property in AppDelegate.h as you would in any other class:
#property (nonatomic) int randomNumber;
Then import AppDelegate.h in class you would like access this variable.
And write following:
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSLog(#"Int: %d", delegate.randomNumber);
Create static variable in AppDelegate.h or swift. I did it in swift for this answer.
static var myInt = 2
Then on the ViewController or other class just access it.
override func viewDidLoad() {
super.viewDidLoad()
println(AppDelegate.myInt)
}
You can do the instance variable but if you do so like:
var myInt = 2
Then you need a object of AppDelegate to get the value:
println(AppDelegate().myInt)
I prefer the first one as i don't have to create object but here the AppDelegate is a singleton so either way is just fine.

Ios tabed application #property

I am a beginner in Ios developement and I'm developping a tabed application but I would like a property accessible from each view so i write this in appDelegate:
#property (strong, nonatomic) NSString *test;
But now I don't know how to access this string from my firstViewController.m and secondViewController.m
You need to create Object of Appdelegate class like:-
AppDelegate *del = [[UIApplication sharedApplication]delegate];
and used this defined string in any class using del.test. Do not forget to import your Appdelegate class in to particular class that you want to use it's string. And also #synthesize your string in .m appdelegate class as well.
UPDATE:-
If you wish to use Appdelegate Object in particular class then Import App-delegate in to .h file like:-
and .m class in ViewDidLoad

No managedObjectContext defined in my AppDelegate

I'm trying to test my core data scheme. However, it seems I am unable to create the context because it says No visible #interface for 'MyAppDelegate' declares the selector 'managedObjectContext'.
In online tutorials this method seems to be auto-generated when we create the app. However, in my case it doesn't exist.
This is MyAppDelegate:
Header
#import <UIKit/UIKit.h>
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m file
#import "MyAppDelegate.h"
#implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
// Override point for customization after application launch.
return YES;
}
How should I fix this in Xcode 5 with iOS 7?
I think the best way for you is to create a Master-Detail Application with Xcode 5 and don't forget to check Use Core Data :
With that, you will have an AppDelegate.h and an AppDelegate.m configured with a managedObjectContext.
You will have a project configured correctly with Core Data and a .xcdatamodeld to use easily your SQLite database.

Declare project wide variable in Objective C

I was wondering if there is a way to declare a project wide variable like you get when you start a application with a UINavigationController and you can call that navigation controller from anywhere in you application because of how its been declared in the applications Delegate.
I am wanting to create a project (global) var that allows me to call SVProgress hud or dismiss it from anywhere in my application. I have this issue where I load a UIViewController onto the navigation stack, start the SVProgress hud then make a request to my DB.. if I get an error back i need to handle a few things one of them is to be able to dissmiss the SVProgress hud.
This is the code I have so far
AppDelegate.h
#import "SVProgressHUD.h"
#interface MYAPPAppDelegate : UIResponder <UIApplicationDelegate> {
//..
}
#property (strong, nonatomic) SVProgressHUD *svprogressHud;
AppDelegate.m
#synthesize svprogressHud;
not really sure if this is possible hopefully someone out there will be able to help me out.
Here is a link to SVProgress git
One thing you can do from your view controller is grab the app delegate and cast it to the type of your delegate like so:
#import "MYAPPDelegate.h"
MYAPPDelegate *ad = (MYAPPDelegate *)[[UIApplication sharedApplication] delegate];
[ad.svprogressHud displayOrWhatever];
Not really the cleanest but should get the job done.
Just add the header file name into you pre-compiled header (<projectname>_Prefix.pch) file inside of the #ifdef __OBJC__
Like as:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SVProgressHUD.h"
#endif
Then it will give you access to the file across all files.....
Just extending Roberts answer, In order to avoid repeating the code to get the app delegate and then cast it into MYAPPDelegate, I would create a header file/class in your project with a utility function.
Create new header called AppUtils.h
#import "MYAPPAppDelegate.h"
#ifndef AppName_AppUtils_h
#define AppName_AppUtils_h
static inline MYAPPAppDelegate* AppDelegate() {
return (MYAPPDelegate *)[[UIApplication sharedApplication] delegate];
}
#endif
Where ever you need access to these variables import the header AppUtils.h and use the helper function. Example:
#import "AppUtils.h"
...
- (void)foo {
// use app delegate
UIViewController *rootVC = AppDelegate().rootViewController;
}

Resources