【问题标题】:Singleton LocationManager starting updates?Singleton LocationManager 开始更新?
【发布时间】:2011-03-05 14:03:13
【问题描述】:

为什么我的单例中的这个 CLLocationManager 不起作用?我拿了这个代码http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/

根本没有改变他的代码(所以如果我应该在他的代码中添加一些东西,请告诉我)这是我的第一个单身人士。

- (CLLocationManager *)locationManager {

    if (locationManager != nil) {
        return [LocationController sharedInstance].locationManager;
    }

    self.locationManager = [LocationController sharedInstance];
    [LocationController sharedInstance].locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;


    return [LocationController sharedInstance].locationManager;
}
  • (void)viewDidLoad { [超级viewDidLoad];

    // Start the location manager.
    [LocationController sharedInstance].delegate = self;
    //[[self locationManager] startUpdatingLocation];
    
    [[LocationController sharedInstance].locationManager startUpdatingLocation];
    

【问题讨论】:

    标签: iphone objective-c singleton cllocationmanager


    【解决方案1】:

    我不太明白你想在你的 - (CLLocationManager *)locationManager 方法中实现什么。但在 LocationController.h 的 init 函数中,您应该添加如下内容:

    - (id)init
    {
       self = [super init];
       if (self != nil) {
           self.locationManager = [[[CLLocationManager alloc] init] autorelease];
           self.locationManager.delegate = self;
           self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        }
       return self;
    }
    

    当你想调用其他控制器中的单例对象来更新位置时,你应该可以调用:

            [[LocationController sharedInstance].locationManager startUpdatingLocation];
    

    记得在你的控制器中实现委托方法 - (void)locationUpdate:(CLLocation*)location。

    如果我的帖子没有,希望这次能有所帮助。

    【讨论】:

      【解决方案2】:

      也许问题出在委托上。这指向文件,而不是单例。

      【讨论】:

        【解决方案3】:

        假设- (CLLocationManager *)locationManagerLocationController这一行的方法

        self.locationManager = [LocationController sharedInstance];
        

        没有意义,因为您将 LocationController 的单例实例分配给 self.locationManager 类型为 CLLocationManager

        【讨论】:

          【解决方案4】:

          这是我所做的,您可以在 github 上找到完整的示例。

          https://github.com/irfanlone/CLLocationManager-Singleton-Swift

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-03
            • 1970-01-01
            • 2014-03-19
            • 2012-07-05
            相关资源
            最近更新 更多