我使用的最终解决方案:此方法对我来说效果不佳,我只有2个变量必须转移,所以我决定使用NSNotificationCenter发送对象。如果您只有很少的物品要转移,请仅使用它,否则它可能会变得非常混乱!如果您想了解更多相关信息,我建议您查看以下问题:pass NSString variable to other class with NSNotification祝您好运!
我正在尝试从我的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);
}
}
];
}
这就是我从其他View Controller访问代码的方式:
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;
这是我的View Controller .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];
以便位置经理有时间获取该位置。
答案 0 :(得分:2)
您在分配价值时未使用设定者,因此未保留该值。尝试更改:
currentLocation = [NSString stringWithFormat:@"%@", Final];
要
self.currentLocation = [NSString stringWithFormat:@"%@", Final];
为避免混淆,您可以停止在标题中声明实例变量...试试这个:
@interface ForecasterAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@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;
并将其添加到您的实现中
@sysnthesize window=_window, locationManager=_locationManager, FinalQuery = _FinalQuery, currentLocation=_currentLocation, Final=_final;
这是标准方式,如果你尝试
currentLocation = [NSString stringWithFormat:@"%@", Final];
编译器将抛出一个错误,这将阻止您意外设置实例变量。现在,随着这些变化,您的真正问题开始变得明显。您的代码将值设置为
_currentLocation = [NSString stringWithFormat:@"%@",Final];
这会直接设置实例变量,因此不会保留该值。该值可能会在下一个大括号中被抛弃,因为没有任何东西保留它。当您将 self 作为 self.currentLocation =某个值作为前缀时,您将该值传递给保留的setter。然后它应该适用于所有视图控制器:)
希望这会有所帮助,如果您发现它让人困惑,请告诉我,我会尝试将其编辑一下。
答案 1 :(得分:0)
这取决于您定义CurrentLocation
的位置。由于从其他视图控制器访问时没有出现任何错误(我假设),因此您将CurrentLocation
定义为ForecasterAppDelegate.h
文件中的属性。您可能忘记将保留属性添加到CurrentLocation
。或者,如果您使用ARC,则可以使用strong
,如下所示。
@property(strong) NSString *currentLocation