change value of var in AppDelegate - iphone

In my AppDelegate, I have
#import <UIKit/UIKit.h>
#import "CustomerProfile.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) int x;
#end
At class B, I do
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.x = 5;
Then at Class C, I do
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.x = 4;
Eventually, at class D I print out the result of x and x = 5. Should x be 4.
It is confusing me. Please advice me on this issue.
Thanks

In your App delegate method your property x is set to strong (aka retain), you have to set to assign, a int var can't be retained because its not a object:
#property (assign, nonatomic, readwrite) int x; //then #synthesize in the implementation
Second, you have to import the header of your appDelegate (in your B,C,D Classes)
#import "yourAppDelegate.h"
set your appDelegate instance:
yourAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; // or [NSApplication sharedApplication] if your app it is for OS X
then set your x var to the desired value
appDelegate.x = 5 (or whatever)
I tested this in one of my projects and works.

Related

How to use appdelegate variable in view

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?
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!
UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString * str = appDelegate.yourstr;
You can get it with this code:
UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *productName = appDelegate.productname;

Accessing member variable of AppDelegate

I'm trying to wrap my head around singletons and I understand that the App Delegate is essentially a singleton object. I'm trying have some member variables in App Delegate that I can access from any other class. I did this in the App Delegate:
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
int screenwidth;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic) int screenwidth;
Then in the .m I did this:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
...
screenwidth=400; //arbitrary test number
Now I have another class in the project, and it does this in the .h:
#import "AppDelegate.h"
In the .m I have this somewhere:
test=(AppDelegate*)[[[UIApplication sharedApplication] delegate] screenwidth];
However, it claims that "screenwidth" is an instance method that is not found. I also tried this:
test=(AppDelegate*)[[UIApplication sharedApplication] delegate].screenwidth;
This uses the dot syntax since screenwidth was synthesized, but it claims that property screenwidth not found
I'm sure these are basic issues that can be corrected simply. Any help appreciated.
Consider trying:
test=[(AppDelegate*)[[UIApplication sharedApplication] delegate] screenwidth];
I think your two tries are trying to cast the .screenwidth result to an AppDelegate*.
Make sure that you're either providing your own -screenwidth accessor or using the #synthesize directive to get the compiler to provide one:
#synthesize screenwidth
The #property directive is just a promise that accessors for the screenwidth property will be provided. You still have to provide them as described above.
If you want to avoid casting to your AppDelegate class every time, I recommend the following:
In MyAppDelegate.h:
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
+ (MyAppDelegate *)sharedAppDelegate;
#property (nonatomic) int screenwidth;
/* ... */
#end
In MyAppDelegate.m:
#implementation LcAppDelegate
+ (MyAppDelegate *)sharedAppDelegate
{
return (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
}
/* ... */
#end
Of course, you still need to #import "MyAppDelegate.h" in the files where you want to access it:
#import "MyAppDelegate.h"
/* ... */
NSLog(#"the apps screen width: %d", [MyAppDelegate sharedAppDelegate].screenwidth);
BTW, note that you should not use int's and the like in Objective-C code. Instead, use NSInteger, NSUInteger, CGFloat and so on.

Xcode - warning problem

I have a navigation controller that utilizes an if statement to switch between the different views, and when i run it it comes up with a warning on the line:
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
warning: type 'id ' does not conform to the 'UITabBarControllerDelegate' protocol
I'd like some help on how to get rid of this warning.
Here is the whole if statement:
if(indexPath.row == 0)
{
MapDetailController *mapD = [[MapDetailController alloc] initWithNibName:#"MapDetailController" bundle:nil];
self.mapDetailController = mapD;
[mapD release];
mapDetailController.title = [NSString stringWithFormat:#"%#", [moreArray objectAtIndex:row]];
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.moreNavController pushViewController:mapDetailController animated:YES];
}
Thanks
EDIT:
Here is what my AppDelegate looks like (response to answer #2)
#import <UIKit/UIKit.h>
#class MoreNavController;
#interface ROSS_APP_7AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
IBOutlet UITabBarController *tabBarController;
IBOutlet MoreNavController *moreNavController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet MoreNavController *moreNavController;
#end
You probably forgot to have your app delegate conform to the UITabBarControllerDelegate protocol.
To implement it, your appDelegate header should look like this (the #interface declaration is the relevant line):
#import ...
#interface ROSS_APP_7AppDelegate : AppDelegate_Shared <UITabBarControllerDelegate>
{
....
}
#property(nonatomic, retain) .....
#end
You might be using AppDelegate_Shared / AppDelegate_iPhone / AppDelegate_iPad so bear in mind that the above example considers a shared app delegate
EDIT:
After seeing your comment,
Try replacing:
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
With this:
ROSS_APP_7AppDelegate *delegate = (ROSS_APP_7AppDelegate*)[[UIApplication sharedApplication] delegate];
Does typecasting the return like this get rid of your warning?
Have you tried casting the delegate as in:
(id)[[UIApplication sharedApplication] delegate];
???

data between Views UIApplication

i want to share data between views...
i have the appdelegate of tabbar application:
myappdelegate.h
#import <UIKit/UIKit.h>
#interface myappdelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
NSString *result;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (copy , readwrite) NSString *result;
#end
if i want to call with this command, there is the hint: "may not respond"....
myappdelegate *dataCenter = [(myappdelegate *)[UIApplication sharedApplication] delegate]; <<may not respond
dataCenter.result = #"msg";
result_view *resultView = [[result_view alloc] initWithNibName:#"result_view" bundle:nil];
[self.navigationController pushViewController:resultView animated:YES];
[resultView release];
result_view.m
- (void)viewDidLoad
{
myappdelegate *dataCenter = (myappdelegate*)[[UIApplication sharedApplication]delegate];
[label setText:dataCenter.result];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
the program crashes...
Your code is saying that the sharedApplication is of the class myappdelegate, which indeed does not respond to delegate. Do this:
(myappdelegate *)[[UIApplication sharedApplication] delegate];
to remove the warning.
Due to Objective-C's runtime messaging, your current (warning-generating) code won't crash the app. The crash lies somewhere else.
Your first line should be
myappdelegate *dataCenter = (myappdelegate *)[[UIApplication sharedApplication] delegate];
As for the second line, I can't tell what you expect to happen. You don't have a result_array property on your myappdelegate class, so of course you can't set that property.
If you were trying to set the result property, you should have written
dataCenter.result = #"msg";

how to use pagecontrol globally?

In my application i want to use instance of pagecontrol in more then 3 views..so,i want to declare,#property and #synthesize should be in appDelegate..Please give some ideas to do this...
Thanks,
Mano
I never used pagecontrol, but in principle, your code would look like:
in YourAppDelegate .h
#interface YourAppDelegate : NSObject <UIApplicationDelegate> {
...
UIPageControl *pageControl;
...
}
#property (nonatomic, retain) IBOutlet ScrollingViewController *viewController;
YourAppDelegate.m:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pageControl = [[UIPageControl alloc] init]; // or whatever is needed to set up pagecontrol
}
in one of your views:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable