1

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?

0

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0
UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString * str = appDelegate.yourstr;

Comments

0

You can get it with this code:

UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *productName = appDelegate.productname;

2 Comments

it is showing error use of undeclared identifier uiapplicationdelegate
UIApplicationDelegate here i mentioned the name of the app delegate class of your project. #import the appdelegate class of your project into your viewcontroller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.