iphone - Objective-C 将参数传递给委托(delegate)类

标签 iphone objective-c delegates

我有一个 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举个例子。

首先,delegatehelp 的对象另一个类,你应该想到 delegatehelper类,(我知道这并不完全正确)。
所以在 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/

相关文章:

.net - 委托(delegate)返回类型的问题

c# - 将函数作为参数传递给 Action

iphone - iOS 7 Safari : OS locks up for 4 seconds when clicking/focusing on a HTML input

iphone - 将新实体添加到 Core Data 中而不删除以前的版本以避免应用程序崩溃

iphone - 无法摆脱 xib 文件中的组 TableView 背景颜色已弃用警告

ios - 是否可以在 UIButton 上实现 3d touch?

objective-c - 从 objective-c 类调用 swift 类的自定义委托(delegate)会抛出错误

ios - 我们可以为一个 View Controller 使用两个不同的 Xib 吗?

ios - 在哪里可以下载 OS X 10.10 SDK 以在 Xcode 6.3 中运行 Mac 应用程序?

ios - 如何知道是否在 Xcode 中触摸了 .png 的唯一可见区域