我有一个 Xcode 项目,它基本上加载并解析一个 xml 文件,该文件输出靠近用户当前位置的位置(地点)列表。我最初对经度和纬度坐标进行了硬编码,以确保我的 xml 正确解析(确实如此),但现在我尝试传递用户当前位置以确定距离用户最近的位置(类似于商店)发现者)。我有一个处理加载 xml 文件的委托(delegate)类文件和一个处理用户当前位置的 rootViewController 文件。我的问题是我不知道将参数从一个类传递到委托(delegate)类的最佳方法...抱歉,我知道这是基本的,但我正在努力解决它:)
这是我所拥有的基础的两个片段:
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 文件上显示结果 (NSLog),但不能在 XMLAppDelegate.m 文件上显示结果。我需要找出将参数从 RootViewController.m 传递到 XMLAppDelegate.m 的最佳方法?
如果您需要更多信息,请告诉我。
干杯
最佳答案
我会说,像苹果一样去做。假设我们采用 UITableView
举个例子。
首先,delegate
是 help
的对象另一个类,你应该想到 delegate
如helper
类,(我知道这并不完全正确)。
所以在 UITableView
在这种情况下,UITableView 要求它的委托(delegate)做一些工作,并且由于它要求它正在监听 return
到。
当您接到 UITableView
打来的电话时
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
您正在将信息发送回master
委托(delegate)关于 UITableView 中应该有多少部分的信息。
其次,如果仔细观察方法签名,您会发现 master
将自身作为参数传递,因此 delegate
可以问是master
如果需要的话,获取更多信息。
这就是它的工作原理,master
有一个指向它的 delegate
的指针能够问它一些问题,并且 delegate
了解它是master
当它接到电话时。 delegate
可以存储指向它的 master
的指针,但正如您所见,这是没有必要的。
这意味着您始终可以引用 delegate
或发送至master
如果您有引用,发送信息不是问题。
请记住,委托(delegate)应该是向其主人提供信息的人,因此,如果我正确地阅读了您的代码,您就会执行相反的操作。
关于iphone - Objective-C 将参数传递给委托(delegate)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8498985/