i declared a variable NSString productname in appdelegate and assigned value appdelegate.productname = name from a view.Then i tried to get this value from another view.lbl.text=appdelegate.productname. Is this is wrong?
3 Answers
you can declare variables in appdelegate.h file, these variables are global you dont need to make appdelegate object to calling them.
like this -
#import <UIKit/UIKit.h>
@class ViewController;
// these are your variable, both are global.
int anyNumber;
NSString *productname;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) UINavigationController *naviCon;
@end
Now you can use these variables at any where you want to use.
just import the appdelegate.h and use it freely.
#import "ViewController.h"
#import "AppDelegate.h"
this is your first view from where you are assigning the value to appdelegate string.
productname = name; //you can assign it directly, no need to make any object of appdelegate.
now you can use it any where. but remember little thing you have to import
#import "AppDelegate.h"
in your viewcontroller.
Thank you!
Comments
You can get it with this code:
UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *productName = appDelegate.productname;
2 Comments
user1395474
it is showing error use of undeclared identifier uiapplicationdelegate
vtechig
UIApplicationDelegate here i mentioned the name of the app delegate class of your project. #import the appdelegate class of your project into your viewcontroller.