在您的视图逻辑中等待,直到调用didUpdateToLocation或didFailWithError的CoreLocation委托。让这些方法调用/ init你的列表和UI数据填充。
样品控制器:
部首
@interface MyCLController : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
@end
代码
#import "MyCLController.h"
@implementation MyCLController
@synthesize locationManager;
- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // send loc updates to myself
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %@", [newLocation description]);
// FILL YOUR VIEW or broadcast a message to your view.
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", [error description]);
// FILL YOUR VIEW or broadcast a message to your view.
}
- (void)dealloc {
[self.locationManager release];
[super dealloc];
}
@end