2011-12-14 6 views
2

私はXMLプロジェクトを基本的に読み込んで、ユーザの現在地に近い位置(会場)のリストを出力する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.... 
} 

私は結果(NSLog)をRootViewController.mファイルに表示できますが、XMLAppDelegate.mファイルには表示できません。 RootViewController.mからXMLAppDelegate.mにパラメータを渡す最善の方法を見つける必要がありますか?

必要な情報があれば教えてください。

乾杯

+3

applicationDidFinishLaunchingが呼び出される最初のものです。その後にviewDidLoadが発生するため、そこでの変更はappDidFinishLaunchingには何の影響も与えません。 –

+0

他のクラスのappDelegateのオブジェクト/値を使用する必要があります。 – utsabiem

+0

また、currentLocation.coordinate.latitudeは数値(整数または浮動小数点数)です。 NSLogがNSObjectとしてアクセスしようとしていて、クラッシュしてしまったように、0になっていないかのように(null)を得たことは幸いです。 NSLogでは、適切な書式指定子(整数の場合は "%d"、浮動小数点の場合は "%f")を使用する必要があります。 –

答えて

1

私はAppleがそれをやっているようにそれを行う、と言うでしょう。たとえば、UITableViewを例としましょう。

まず、delegatehelp他のクラス、あなたがhelperクラスとしてdelegateを考える必要がありますオブジェクト、(私はこれは完全に真実ではないことを知っている)です。
したがって、UITableViewのケースでは、UITableViewは委任者に何らかの作業を依頼しており、returnを聞いていることを尋ねているためです。
あなたはUITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

からこのコールを取得するとあなたはのUITableViewであるはずどのくらいのセクションについてのデリゲートのmasterに情報を返送しています。

第2に、メソッドシグネチャをよく見ると、masterがパラメータとして渡されていることがわかるので、必要ならばmasterにと尋ねることができます。

だから、これはどのようにこの作品で、masterはそれにいくつかの質問をすることができるようにdelegateだ、とdelegateは、それが呼び出しを受けるときmasterだ知って取得へのポインタを持っています。 delegateにはmasterへのポインタを格納できますが、それは必要ではありません。

これは、あなたが常にdelegateまたはmasterへの参照を持っていることを意味し、それについての参照がある場合は、情報の送信に問題はありません。

デリゲートはマスターに情報を提供するものでなければならないことを覚えておいてください。正しくコードを読んでいれば逆のことをしています。

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 

は、この情報がお役に立てば幸いです。

+0

両方の優れた反応!この両方にあなたの助けをいただきありがとうございます。 – user1068733

関連する問題