2012-01-20 6 views
0

UIViewControllerの代わりにNSObjectのサブクラスであるシングルトンクラスにCLLocationManagerデリゲートメソッドを配置することはできますか?私は、Idがアプリケーションデリゲートからシングルトンを呼び出し、UIがCLLocationManagerは常にnullです - デリゲートの実装はシングルトンクラスです(UIViewControllerのサブクラスの代わりにNSObjectのサブクラス)

をロードしている間の座標を取得を開始するために好きなように私はlocationcontrollerクラスを持っていると私はそれに

static locationController *sharedLocController = NULL; 
+(locationController *) getSharedController 
{ 
    if (sharedLocController !=nil) 
    { 
     NSLog(@"locationController has already been created....."); 
     return sharedLocController; 
    } 
    @synchronized(self) 
    { 
     if (sharedLocController == nil) 
     { 
      sharedLocController = [[self alloc] init]; 

     } 
    } 
    return sharedLocController; 
} 
//============================================================================== 
+(id)alloc 
{ 

    @synchronized([locationController class]) 
    { 

     NSAssert(sharedLocController == nil, @"Attempted to allocate a second instance of a sharedLocMgr singleton."); 
     sharedLocController = [super alloc]; 
     return sharedLocController; 
    } 
    return nil; 
} 
//============================================================================== 
-(id)init 
{ 
    self = [super init]; 

    if(sharedLocController !=nil) 
    { 

     if(!self.locMgr) 
     { 
      [self initLocationManager]; 

     } 
    } 



    return sharedLocController; 
} 
//============================================================================== 
-(void)initLocationManager 
{ 


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

    self.locMgr.delegate = self; 
    self.locMgr.distanceFilter = kCLDistanceFilterNone; 
    self.locMgr.desiredAccuracy = kCLLocationAccuracyBest; 
    [self.locMgr startUpdatingLocation]; 
    NSLog(@"location manager object %@", locMgr); 
} 
を次の初期化コードを持ってこれをやりたいです

問題は、self.locMgrオブジェクトが常にnullであることです。 ありがとう

答えて

0

あなたのalloc関数を削除してください。これは必要ではなく、nilを返すので、このinit呼び出しがnilポインタで呼び出されていることを意味します。

sharedLocController = [[self alloc] init]; 
+0

ありがとう – banditKing

0

はい、NSObjectサブクラスに入れることはできますが、これはシングルトンの可能性があります。私は次のようなものを使用します:

@interface MyCLController : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
} 

... 

@end 

ただし、それはシングルトンではありません。