Getter Accessor无法运行,iphone sdk Objective C

时间:2009-12-05 13:21:03

标签: objective-c

我正在尝试执行一个简单的操作,我在这个程序中做了很多。我试图在应用程序完成启动后启动CLLocationManager,并在更新发生后获取当前位置。我的应用程序工作在它获取当前位置,当我NSLog当前位置它工作正常。问题是我试图将这个位置从一个对象传递到另一个对象,并且一旦到达那里它就没有值。

我一直在查找访问器方法,一切似乎都正确,因为我在我的程序中都有这些类型的操作。我试图效仿很多例子,但似乎都没有。

这是一些代码。

这是CLLocationManagerDelegate的.h文件。

@interface YogaAllianceAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate> {

CLLocationManager *locationManager;
    CLLocation *mycurrentLocation;

    float curLats;
    float curLons;
    BOOL locationDidUpdate;

    UIWindow *window;

    YogaAllianceViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;  
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) float curLats;
@property (nonatomic, assign) float curLons;
@property (nonatomic, assign) CLLocation *mycurrentLocation;
@property (nonatomic, retain) IBOutlet YogaAllianceViewController *viewController;
@end

这是.m文件

@synthesize viewController, locationManager, curLats, curLons, mycurrentLocation;





- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.locationManager = [[[CLLocationManager alloc]init] autorelease];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];


    locationDidUpdate = NO;
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}


-(void)locationManager: (CLLocationManager *)manager
   didUpdateToLocation: (CLLocation *)newLocation
          fromLocation: (CLLocation *)oldLocation{

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"currentLocationDidUpdate" object: self];

    [locationManager stopUpdatingLocation];



    NSLog(@"Core Location says location available");


    self.curLats = newLocation.coordinate.latitude;
    self.curLons = newLocation.coordinate.longitude;

    self.mycurrentLocation  = newLocation;


    NSLog(@"current position is then %@", [mycurrentLocation description]);

}

此时它有效。我得到了打印出的位置和所有内容,但我需要在另一个对象中的位置,似乎我所有尝试重写点符号都失败了。 这里是 。 h我需要它的对象。

@interface School_Location_List_Controller : UIViewController {


    CLLocation* currentLocation;    

}

- (IBAction) btnBack_Clicked:(id)sender;


@property (nonatomic, assign) CLLocation* currentLocation;

这是.m

@implementation School_Location_List_Controller

@synthesize destinations, closeSchools, currentLocation;



-(void) viewWillAppear: (BOOL)animated {


    NSLog (@"Registered for location events");

    YogaAllianceAppDelegate *updated = [[YogaAllianceAppDelegate alloc]init];


    currentLocation = updated.mycurrentLocation ;



    NSLog(@"current position here in the passed object is %@", [currentLocation description]);


    [super viewWillAppear:animated];
}

正是在这一点上,我很困惑。当我打印出来时,它只是说currentLocation是空的。它就像从来没有得到数据..

提前谢谢你们。

2 个答案:

答案 0 :(得分:0)

你的问题就在这一行:

YogaAllianceAppDelegate *updated = [[YogaAllianceAppDelegate alloc]init];

在这里,您要分配和初始化一个全新的对象,一个完全不同于应用程序现有委托的对象。如果要获取已存在的应用程序委托,请使用:

YogaAllianceAppDelegate *updated = (YogaAllizanceAppDelegate *)[[UIApplication sharedApplication] delegate];

答案 1 :(得分:0)

我不确定这是不是你的问题,但试试这个:mycurrentLocation属性为assign,这意味着在设置时,你只有一个指向对象的指针。当该位置被释放时,mycurrentLocation无法保留该位置。

我建议使用retaincopy