在viewDidLoad之前访问属性

时间:2014-02-16 16:29:25

标签: ios objective-c

在以下方法中,我保存了一个位置的当前temp,我希望能够访问类中任何位置的temp。

[_weatherAPI currentWeatherByCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) withCallback:^(NSError *error, NSDictionary *result) {
    downloadCount++;
    if (downloadCount > 1)
        if (error) {
        }
    [_locationManager stopUpdatingLocation];

    self.tempLabel.text = [NSString stringWithFormat:@"%.0f°",
                           [result[@"main"][@"temp"] floatValue] ];

    self.locationLabel.text = [NSString stringWithFormat:@"%@",
                               result[@"name"]
                               ];
    self.summaryLabel.text = [NSString stringWithFormat:@"%@",
                              result[@"weather"][0][@"main"] ];

    [self setSaveTemp:result[@"main"][@"temp"]];


}];

然后我将其保存到属性

@property (strong, nonatomic) NSString *saveTemp;

然后将其保存到此。

-(void)setSaveTemp:(NSString *)saveTemp {
_saveTemp = saveTemp;
}

在我的viewDidLoad中,我正在调用所有内容

_weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@""];
[_weatherAPI setLangWithPreferedLanguage];
[_weatherAPI setTemperatureFormat:kOWMTempCelcius];

NSLog(@"%@", _saveTemp);

但是这会在_weatherAPI currentWeatherByCoordinate之前调用viewDidLoad时返回null,我能做些什么才能使_saveTemp不等于null而是实际值?

1 个答案:

答案 0 :(得分:0)

我认为这个api将进行服务器调用,因此获取json数据需要时间。 一旦json数据可用,它就会执行回调块。

saveTemp变量最初为nil,您将在完成块中进行设置。

所有你能做的就是等到下载完成。如果你想在下载后执行一些方法,那么在callBack块中调用该方法。所以,这样你的应用就可以知道下载已经完成。