我使用的最終解決方案:這種方法對我來說效果並不好,我只有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];
,以便位置管理員有一定的時間來獲取位置。
你的「'CurrentLocation'」屬性聲明在你的「'.h'」文件中看起來像什麼?此外,將「CurrentLocation」重命名爲「currentLocation」。目標C命名約定是變量名稱應該永遠不能以大寫字母開頭。 – 2012-07-11 20:08:01
什麼是你的「CurrentLocation」的實現樣子。我沒有看到你將它設置爲「self.CurrentLocation」,因此可能該值沒有被保留。如果您想將該值公開給其他視圖控制器,那麼它需要保留屬性IMO。 – jerrylroberts 2012-07-11 20:08:12
NSLog AAA語句返回什麼?請提供.h文件。 – 2012-07-11 20:27:06