FINAL SOLUTION THAT I USED: This method did not work out very well for me, and I only had 2 variables which had to be transferred so I decided to use NSNotificationCenter to send the objects. Please use this ONLY if you have very little objects to transfer, or else it could get extremely messy! If you want to learn more about this, I recommend that you check out this question: pass NSString variable to other class with NSNotification Good Luck!
I’m trying to access data from my App Delegate, but I’m always getting a (null).
EDIT: If anyone wants the full code, here it is: http://pastebin.com/hNUPMcvB
Here’s the code in my AppDelegate:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Stop Location Services
[locationManager stopUpdatingLocation];
// Some Reverse Geocoding...
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
City = [[placemark locality] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
State = [[placemark administrativeArea] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
Final = [NSString stringWithFormat:@"%@,%@", City, State];
currentLocation = [NSString stringWithFormat:@"%@", Final];
[self everythingelse];
NSLog(@"AAA%@", currentLocation);
}
}
];
}
This is how I’m accessing the code from my other View Controller:
ForecasterAppDelegate *BossMan = (ForecasterAppDelegate *)[[UIApplication sharedApplication] delegate];
CurrentLocation = BossMan.currentLocation;
NSLog(@"CCC%@", CurrentLocation);
It always logs as CCC(null). In my AppDelegate, it comes out right, but in the other view controller its not. I’m either making a small mistake or a huge one, any help would be great!
EDIT: Here are my header files:
My App Delegate .h:
@interface ForecasterAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate> {
UIStoryboard *storyboard;
NSString *City, *State, *Final, *currentLocation;
NSMutableString *FinalQuery;
CLLocationManager *locationManager;
CLPlacemark * myPlacemark;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic, retain) NSMutableString *FinalQuery;
@property (strong, nonatomic) NSString *currentLocation;
@property (nonatomic, retain) NSString *Final;
and here is my View Controller .h file:
@interface View1 : UIViewController <CLLocationManagerDelegate, LocationDamn> {
CLLocationManager *locationManager;
CLPlacemark * myPlacemark;
NSString *launchpath;
NSString *City, *State, *condition, *Final, *degreesign, *changeLocation, *currentLocations;
}
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic,retain) IBOutlet UILabel *currentTempLabel, *highTempLabel, *lowTempLabel, *conditionsLabel, *cityLabel, *day2c, *day3c, *currentconditions;
@property (nonatomic,retain) IBOutlet NSString *currentLocations;
@end
In my viewdidload of my ViewController, I have this:
[self performSelector:@selector(DoSomethingCool) withObject:nil afterDelay:1.5];
so that the location manager has some time to get the location.
You’re not using the setter when assigning your value therefore it’s not retaining the value. Try changing:
To
To avoid confusion you can stop declaring your instance variables in your headers… try this:
and add this to your implementation
That’s the standard way and then if you try
The compiler will throw an error, which will prevent you from setting the instance variable by accident. Now, with those changes, your real issue starts to become apparent. Your code was setting the value like
This sets the instance variable directly so the value does not get retained. The value is probably getting thrown away at the next curly bracket because nothing has retained it. When you prefix it with self as self.currentLocation = some value then you are passing the value to a setter which retains. Then it should be available for all your view controllers 🙂
Hope this helps, if you find it confusing let me know and I’ll try to edit it down a little.