从其他视图控制器访问数据(空)

问题描述:

我使用的最终解决方案:这种方法对我来说效果并不好,我只有2个变量需要传输,因此我决定使用NSNotificationCenter发送对象。如果你只有很少的东西要转移,请使用它,否则它会变得非常混乱!如果您想了解更多信息,我建议您查看以下问题:pass NSString variable to other class with NSNotification祝您好运!从其他视图控制器访问数据(空)

我试图从我的应用程序委托访问数据,但我总是得到一个(空)。

编辑:如果有人想完整的代码,那就是: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(空)。在我的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 

在我的viewDidLoad中我的ViewController,我有这个:

[self performSelector:@selector(DoSomethingCool) withObject:nil afterDelay:1.5]; 

,以便位置管理员有一定的时间来获取位置。

+1

你的“'CurrentLocation'”属性声明在你的“'.h'”文件中看起来像什么?此外,将“CurrentLocation”重命名为“currentLocation”。目标C命名约定是变量名称应该永远不能以大写字母开头。 – 2012-07-11 20:08:01

+3

什么是你的“CurrentLocation”的实现样子。我没有看到你将它设置为“self.CurrentLocation”,因此可能该值没有被保留。如果您想将该值公开给其他视图控制器,那么它需要保留属性IMO。 – jerrylroberts 2012-07-11 20:08:12

+0

NSLog AAA语句返回什么?请提供.h文件。 – 2012-07-11 20:27:06

分配你的价值,因此它没有保留价值,当你不使用的制定者。尝试改变:

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.currentLocation =某个值然后你将值传递给保留的setter。那么它应该可用于所有你的视图控制器:)

希望这会有所帮助,如果你觉得困惑让我知道,我会尽量编辑它一点。

+0

这里只是有点混乱,所以我将它设置为self.currentLocation,并且得到了相同的(null)错误,但我认为我在这里做了一些小小的错误。将合成添加到我的实现后,代码中的所有内容都应该是_(insertobjecthere)?非常感谢你的帮助,我真的很感激! – sridvijay 2012-07-11 23:57:05

+0

等号的左侧是属性名称,右侧是_propertyname。这是一个暗示其私人性质的公约。不要忘记位置管理器是异步的,所以它将为空,直到它在您的位置为零。 – jerrylroberts 2012-07-12 00:02:09

+0

是的,我有一个延迟集(编辑问题),以便位置管理器有时间获取位置。我不知道你是什么意思,这是一个暗示它是私人的惯例,你能解释一下吗? – sridvijay 2012-07-12 00:03:56

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

@property(strong) NSString *currentLocation

+0

我现在拥有它(强,非原子)。有任何想法吗? – sridvijay 2012-07-11 23:13:37