-1

我使用的最终解决方案:这种方法对我来说效果不佳,我只有 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];

以便位置管理员有时间获取位置。

4

2 回答 2

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 = some value时,您会将值传递给保留的 setter。然后它应该可用于您的所有视图控制器:)

希望这会有所帮助,如果您觉得它令人困惑,请告诉我,我会尝试对其进行一些编辑。

于 2012-07-11T23:47:52.650 回答
0

这取决于您在哪里定义CurrentLocation. 由于您在从其他视图控制器(我假设)访问时没有收到任何错误,因此您在文件中定义CurrentLocation为一个属性。ForecasterAppDelegate.h您可能忘记将保留属性添加到CurrentLocation. 或者,如果您使用的是 ARC,那么您可以strong如下使用。

@property(strong) NSString *currentLocation

于 2012-07-11T22:13:37.210 回答