我有一个Xcode项目,它基本上会加载和解析一个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....
}
我能够显示在RootViewController.m文件,但不能在XMLAppDelegate.m文件的结果(NSLog的)。我需要找出将参数传递给从RootViewController.m到XMLAppDelegate.m的最佳方式?
请让我知道是否有任何您需要的信息。
干杯
applicationDidFinishLaunching是第一件会被调用的,viewDidLoad会在它后面发生,所以这里所做的更改将不会对appDidFinishLaunching产生任何影响。 – 2011-12-14 02:50:03
您应该在其他类中使用appDelegate的objects/values,而不是相反。 – utsabiem 2011-12-14 03:17:50