【问题标题】:CLLocation Manager Delegate is not getting called in iPhone OS 4.0CLLocation Manager Delegate 在 iPhone OS 4.0 中没有被调用
【发布时间】:2010-07-06 06:36:48
【问题描述】:

我必须在 iPhone OS 4.0 中运行我的应用程序。 (在模拟器中)。

-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self newLocationUpdate];
}

-(void)newLocationUpdate
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation 
{
    [locationManager stopUpdatingLocation];
}

在这个 CLLocationManager 委托方法没有被调用。我们应该进行哪些更改才能调用委托方法?

【问题讨论】:

    标签: iphone objective-c cllocationmanager


    【解决方案1】:

    我怀疑您的“locationManager”实例过早释放。

    它是一个属性吗? 如果是,则更改为:

    locationManager = [[CLLocationManager alloc]] init];
    

    到:

    self.locationManager = [[CLLocationManager alloc] init];
    

    并确保声明了属性:

    @property (nonatomic, retain) CLLocationManager * locationManager;
    

    别忘了稍后在适当的时候发布它。

    【讨论】:

    • 我同意从长远来看,保留属性会使事情更易于维护,但如果您只是将保留计数为 1(分配、初始化且无自动释放)的位置管理器分配给保留的财产,它最终会保留 2 并且您手上有泄漏。所以使用“自我”。使用保留属性的方法并将自动释放添加到该 alloc-init 的末尾。
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多