私はXMLプロジェクトを基本的に読み込んで、ユーザの現在地に近い位置(会場)のリストを出力するXMLファイルを解析します。私はもともと、私のXMLが正しく解析されていることを保証するために経度と緯度の座標をハードコードしましたが、今はユーザーに現在の場所を渡して、ユーザーに最も近い場所を判断しようとしています(ストアファインダ)。私は、XMLファイルのロードを処理するデリゲートクラスファイルと、ユーザーの現在位置を扱うrootViewControllerファイルを持っています。私の問題は、あるクラスのパラメータをデリゲートクラスに渡す最善の方法を知らないということです。残念ですが、基本的なことは分かっていますが、私はそれに苦しんでいます:)Objective-Cデリゲートクラスへのパラメータの受け渡し
ここでは、私が持っているものの:
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Get Current Location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
NSLog(@"longt Value: %@", longt);
}
XMLAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//This code is incorrect (displays 'null') I want to display the values defined in the locationManager Method on RootViewController.m
NSLog(@"lat Value: %@", currentLocation.coordinate.latitude);
// Longitude and Latitude codes are hard-coded. I want to change this to dynamic values which are defined in the locationManager Method on RootViewController.m
NSURL *url = [[NSURL alloc] initWithString:@"http://www.domain.com/phpsqlsearch_genxml.php?lat=-33.870017&lng=151.206547&radius=25"];
//More code here....
}
私は結果(NSLog)をRootViewController.mファイルに表示できますが、XMLAppDelegate.mファイルには表示できません。 RootViewController.mからXMLAppDelegate.mにパラメータを渡す最善の方法を見つける必要がありますか?
必要な情報があれば教えてください。
乾杯
applicationDidFinishLaunchingが呼び出される最初のものです。その後にviewDidLoadが発生するため、そこでの変更はappDidFinishLaunchingには何の影響も与えません。 –
他のクラスのappDelegateのオブジェクト/値を使用する必要があります。 – utsabiem
また、currentLocation.coordinate.latitudeは数値(整数または浮動小数点数)です。 NSLogがNSObjectとしてアクセスしようとしていて、クラッシュしてしまったように、0になっていないかのように(null)を得たことは幸いです。 NSLogでは、適切な書式指定子(整数の場合は "%d"、浮動小数点の場合は "%f")を使用する必要があります。 –