Objective-C将参数传递给委托类

时间:2011-12-14 02:44:54

标签: iphone objective-c delegates

我有一个Xcode项目,它基本上加载并解析一个xml文件,该文件输出一个列表,列出一个靠近用户当前位置的位置(场所)列表。我最初硬编码经度和纬度坐标以确保我的xml正确解析(它是),但现在我试图传递用户当前位置以确定最接近用户的位置(类似于商店发现者)。我有一个处理加载xml文件的委托类文件和一个处理用户当前位置的rootViewController文件。我的问题是我不知道将参数从一个类传递给委托类的最佳方法...对不起,我知道它是基本的,但我正在努力:)

这里有两个基于我所拥有的片段:

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的最佳方法吗?

如果您需要更多信息,请告诉我。

干杯

2 个答案:

答案 0 :(得分:1)

我会说,就像苹果公司这样做。我们以UITableView为例。

首先,delegatehelp另一个类的对象,您应该将delegate视为helper类,(我知道这不完全正确) )。
所以在UITableView的情况下,UITableView要求它的委托做一些工作,因为它要求它正在监听return到。
当您从UITableView

接听此电话时
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

您正在向代表的master发回有关UITableView中应该有多少部分的信息。

其次,如果仔细查看方法签名,您会看到master将自己作为参数传递,因此delegate可以询问master以获取更多信息。需要的。

这就是这项工作的方式,master指向它的delegate可以向它提出一些问题,delegate了解它master当它接到电话时。 delegate可以存储指向master的指针,但正如您所见,没有必要。

这意味着您始终引用delegatemaster,如果您有参考,则发送信息不是问题。

请记住,委托应该是向其主人提供信息的委托,所以如果我正确地读了你的代码,那你就是反过来的。

答案 1 :(得分:1)

你的课程看起来像这样,能够彼此沟通:

RootViewController.h:

#import <Foundation/Foundation.h>
#import <CoreLoation/CoreLocation.h>
#import <UIKit/UIKit.h>
#import "XMLAppDelegate.h"

@interface RootViewController: UIViewController <CLLocationManagerDelegate, CustomDelegateProtocol> {
    float lon, lat;
}

@end

RootViewController.m:

#import "RootViewController.h"

@implementation RootViewController

// super

- (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];

}

// CLLocationManagerDelegate

- (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);
}

// CustomDelegateProtocol

@synthesize lon = lon, lat = lat;

@end

XMLAppDelegate.h:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@protocol CustomDelegateProtocol

@property (assign) float lon;
@property (assign) float lat;

@end

@interface XMLAppDelegate: NSObject <UIApplicationDelegate> {
    // whatever instance variables you need, put them here
    id <CustomDelegateProtocol> delegate;
}

@end

XMLAppDelegate.m:

#import "XMLAppDelegate.h"

@implementation XMLAppDelegate

/*
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // BTW: don't use this to obtain location info if you're NOT setting up your UI from code...
    // It has (conceptually, but also practically) be done in your view controller.
    // That's why it is called a view controller: it "glues" view and logic code together
    // and that way you won't even need the delegate
    // I also recommend using the -application:didFinishLaunchingWithOptions: method. This one is deprecated.
}
*/

- (BOOL) application:(UIApplication *)app didFinishLaunhingWithOptions:(NSDictionary *)launchOpts {
    // set up initial stuff, etc...
    // and if you *really, badly* want to use the delegation model, here you are:
    NSString *urlString = [[NSString alloc] initWithFormat: @"http://www.domain.com/phpsqlsearch_genxml.php?lat=-%f&lng=%f&radius=25", self.delegate.lat, self.delegate.lon];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    [urlString release];

    // do whatever you want to 


    return YES;
}

@end

希望这有帮助。