我使用的最终解决方案:这种方法对我来说效果不佳,我只有 2 个必须传输的变量,所以我决定使用 NSNotificationCenter 来发送对象。请仅在您要传输的对象非常少的情况下使用它,否则它可能会变得非常混乱!如果你想了解更多,我建议你看看这个问题:pass NSString variable to other class with NSNotification Good Luck!
我正在尝试从我的 App Delegate 访问数据,但我总是得到一个(null)。
编辑:如果有人想要完整的代码,这里是: http: //pastebin.com/hNUPMcvB
这是我的 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);
}
}
];
}
这就是我从其他视图控制器访问代码的方式:
ForecasterAppDelegate *BossMan = (ForecasterAppDelegate *)[[UIApplication sharedApplication] delegate];
CurrentLocation = BossMan.currentLocation;
NSLog(@"CCC%@", CurrentLocation);
它始终记录为 CCC(null)。在我的 AppDelegate 中,它是正确的,但在另一个视图控制器中它不是。我要么犯一个小错误,要么犯一个大错误,任何帮助都会很棒!
编辑:这是我的头文件:
我的应用程序代表 .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;
这是我的视图控制器 .h 文件:
@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
在我的 ViewController 的 viewdidload 中,我有这个:
[self performSelector:@selector(DoSomethingCool) withObject:nil afterDelay:1.5];
以便位置管理员有时间获取位置。