기본적으로 사용자의 현재 위치에 가까운 위치 (장소) 목록을 출력하는 XML 파일을로드하고 구문 분석하는 Xcode 프로젝트가 있습니다. 원래 경도와 위도 좌표를 하드 코딩하여 내 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로 액세스하려고 시도했을 때 충돌이 발생했을 것입니다. NSLog에서는 적절한 형식 지정자 (정수의 경우 "% d", 부동 소수점의 경우 "% f")를 사용해야합니다. –