2012-07-11 68 views
-1

我使用的最終解決方案:這種方法對我來說效果並不好,我只有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

回答

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.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

0

這取決於你在哪裏定義CurrentLocation。由於從另一個視圖控制器(我假設)訪問時沒有發生任何錯誤,因此您在ForecasterAppDelegate.h文件中將CurrentLocation定義爲屬性。您可能忘記將保留屬性添加到CurrentLocation。或者,如果您使用的是ARC,則可以使用strong,如下所示。

@property(strong) NSString *currentLocation

+0

我現在擁有它(強,非原子)。有任何想法嗎? – sridvijay 2012-07-11 23:13:37

相關問題