Iphone Objective-C将参数传递给委托类
我有一个Xcode项目,它基本上加载和解析一个xml文件,该文件输出一个列表,一个靠近用户当前位置的位置(场馆)列表。我最初硬编码经度和纬度坐标,以确保我的xml正确解析(事实上是这样),但现在我尝试将当前位置传递给用户,以确定离用户最近的位置(类似于商店查找器)。我有一个委托类文件来处理xml文件的加载,还有一个rootViewController文件来处理用户的当前位置。我的问题是,我不知道将参数从一个类传递到委托类的最佳方法。。。抱歉,我知道这是基本的,但我正在努力:) 这里是我所拥有的基础的两个片段: RootViewController.m XMLAppDelegate.m 我能够在RootViewController.m文件上显示结果(NSLog),但不能在XMLAppDelegate.m文件上显示。我需要找出将参数从RootViewController.m传递到XMLAppDelegate.m的最佳方法 如果您需要更多信息,请告诉我Iphone Objective-C将参数传递给委托类,iphone,objective-c,delegates,Iphone,Objective C,Delegates,我有一个Xcode项目,它基本上加载和解析一个xml文件,该文件输出一个列表,一个靠近用户当前位置的位置(场馆)列表。我最初硬编码经度和纬度坐标,以确保我的xml正确解析(事实上是这样),但现在我尝试将当前位置传递给用户,以确定离用户最近的位置(类似于商店查找器)。我有一个委托类文件来处理xml文件的加载,还有一个rootViewController文件来处理用户的当前位置。我的问题是,我不知道将参数从一个类传递到委托类的最佳方法。。。抱歉,我知道这是基本的,但我正在努力:) 这里是我所拥有的基
干杯我会说,像苹果那样做吧。假设我们以
UITableView
为例
首先,委托
是将帮助
其他类的对象,您应该将委托
视为帮助者
类(我知道这并不完全正确)。因此,在
UITableView
案例中,UITableView要求其委托人做一些工作,并且因为它要求它正在侦听返回到的内容。
当您从UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
您正在将有关UITableView中应包含多少节的信息发送回代理的主控
其次,如果仔细观察方法签名,您将看到主签名
正在将自身作为参数传递,因此委托
可以在需要时询问它的主签名
,以获取更多信息
这就是它的工作原理,master
有一个指向它的delegate
的指针,以便能够问它一些问题,delegate
在接到呼叫时知道它是master
。委托
可以存储指向其主控
的指针,但如您所见,它不是必需的
这意味着您总是有对委托
或主控
的引用,如果您有对它的引用,则发送信息不是问题
请记住,委托应该是向其主控程序提供信息的人,因此,如果我正确地阅读了您的代码,您正在执行相反的操作。您的类将看起来像这样,并具有相互通信的能力:
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
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
希望这能有所帮助。ApplicationIDFinishLaunching是第一个被调用的东西,viewDidLoad将在它之后发生,因此在那里所做的更改不会对AppDidFinishLaunching产生任何影响。您应该在其他类中使用appDelegate的对象/值,而不是相反。此外,currentLocation.coordinate.latitude是一个数字(整数或浮点)。幸运的是,您得到了(null),就好像它不是0一样,NSLog会尝试作为NSObject访问它,并且会崩溃。您应该使用适当的格式说明符(“%d”表示整数,“%f”表示浮点)在NSLog中。为你们的回复干杯,我有一些工作要做-但感谢你们为我指明了正确的方向!两个都是非常好的回复!感谢你们在这方面的帮助。
#import <Foundation/Foundation.h>
#import <CoreLoation/CoreLocation.h>
#import <UIKit/UIKit.h>
#import "XMLAppDelegate.h"
@interface RootViewController: UIViewController <CLLocationManagerDelegate, CustomDelegateProtocol> {
float lon, lat;
}
@end
#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
#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
#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