2011-12-14 37 views
2

我有一个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的最佳方式?

请让我知道是否有任何您需要的信息。

干杯

+3

applicationDidFinishLaunching是第一件会被调用的,viewDidLoad会在它后面发生,所以这里所做的更改将不会对appDidFinishLaunching产生任何影响。 – 2011-12-14 02:50:03

+0

您应该在其他类中使用appDelegate的objects/values,而不是相反。 – utsabiem 2011-12-14 03:17:50

回答

1

我会说,像苹果公司那样做。假设我们以UITableView为例。

首先,delegate是将help的其他类,你应该想到的delegatehelper类的对象,(我知道这是不完全正确)。
因此,在UITableView的情况下,UITableView要求它委托做一些工作,并且因为它要求它正在监听return
当你从UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

此调用您发回信息给代表的master多少部分应该在UITableView的。其次,如果仔细观察方法签名,您会看到master将自己作为参数传递,因此如果需要,delegate可以询问它的master以获取更多信息。

因此,这是这项工作怎么样,master有一个指向它的delegate能够问它一些问题,以及delegate了解它的master,当它得到调用。 delegate可以存储一个指向它的指针master,但正如你所看到的,这是没有必要的。

这意味着您总是参考delegatemaster,如果您有参考,发送信息不是问题。

记住,委托应该是一个提供信息给它的主人,所以如果我正确读取你的代码,你正在做的倒数。

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 

希望这有助于。