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.
Related
For Example :
I have two ViewController, A and B , A have
#Property(copy,nonatomic)NSString *test;
if A push to B How can update test of A, I have see some answer about Double ** and point.but I don‘t know 。Thank you!
You can use singleton class for that.
Use AppDelegate for that.
Make a NSString property in AppDelegate and update the value of this NSString from AppDelegate when you will move from A to B.
In viewWillAppear() of A assign the value of NSString from AppDelegate to that in A(test)
In AppDelegate.h
#property (strong, nonatomic) NSString *value;
In ViewController A
#import "AppDelegate.h"
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.test = appDelegate.value;
}
When push to ViewController B:
You can assign new value to AppDelegate NSString as:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.value = #"YOUR NEW VALUE";
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.
I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
I have surfed quite a lot about this. But the results that I've got tells me that either I should use appDelegate or singleton etc.
All of which produces the same result. i.e I am able to share a variable in different ViewControllers but as soon as the ViewController gets changed , the variable looses it's value.
For example I used a variable named myVar of type int. I declared it in AppDelegate and then I'm able to use it in all the ViewControllers with the help of AppDelegate. But as soon as I move from A ViewController to B ViewController the value of the myVar variable gets "0" again. I don't want that . I want the variable to hold it's value. And I don't want to pass this data with the help of pushViewController etc. Please suggest me a good solution.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, assign) int myVar;
#end
FirstViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate* app = (AppDelegate*)[UIApplication sharedApplication].delegate;
app.myVar = 1;
NSLog(#"%d",app.myVar); //Shows "1" in Log
}
SecondViewController.m
- (IBAction)pressButton:(id)sender{
AppDelegate * app = (AppDelegate*)[UIApplication sharedApplication].delegate;
NSLog(#"%d",app.myVar); // Shows "0" in Log (But I want it to show "1" as I have already set "myVar" value as "1" in my FirstViewController)
}
Your #property (strong, assign) int myVar; is declaring both strong and assign. As an int, it should only be assign, not strong. strong would be used for Objective-c objects, assign for primitive C properties.
I think what you want is the following:
#property (assign, nonatomic) int myVar;
First of all check your code there are some error
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, assign) int myVar;
And check it gives same value in FirstViewController.m and SecondViewController.m
if your var is a NSString, you can simple use the [copy] protocol. e.g.
NSString *eString = [cString copy];
If it is an object, you need to implement the [copy] protocol for that class.
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.
}