アプリケーションが実行されたときにユーザーの場所を取得しています。場所の変数が @(null) の場合、if ステートメントを防ぎます。My FirstViewController.h:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != NULL) {
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];
}
}
私のSecondViewControllerで:
-(void) GettingGlobalVariables{
NSString *UsersCurrentLatitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
NSString *UsersCurrentLongitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] longitude];
NSNumberFormatter * singsingsing = [[NSNumberFormatter alloc] init];
[singsingsing setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumberLatitude = [singsingsing numberFromString:UsersCurrentLatitude];
NSNumberFormatter * sheCameToMeOneMorning = [[NSNumberFormatter alloc] init];
[sheCameToMeOneMorning setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumberLongitude = [sheCameToMeOneMorning numberFromString:UsersCurrentLongitude];
NSLog(@"mynumberlatitude:%@,mynumberlongitude:%@",myNumberLongitude,myNumberLatitude);
if (!myNumberLatitude) {
NSLog(@"location variables are null");
TabBar *TB =[[TabBar alloc]init];
[TB performSelector:@selector(viewDidLoad)];
[TB performSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)];
[self performSelector:@selector(GettingGlobalVariables)];
}
}
このif文で、変数がnullのときアプリクラッシュ。なにか提案を?