2009-12-07 31 views
7

Tôi có một ứng dụng có thanh tab và 3 tab. Vị trí hiện tại của người dùng sẽ cần được biết đến trên bất kỳ tab nào trong số ba tab. Nơi tốt nhất để triển khai CLLocationManager có trong ứng dụng đại diện trong trường hợp này không?Nơi triển khai CLLocationManager

Có được không (thực hành tốt?) Để đặt các phương thức ủy nhiệm CLLocationManager vào tệp ủy quyền ứng dụng m?

Bạn đề nghị tôi đặt số CLLocationManager ở đâu khi tôi gọi -startUpdatingLocation từ một trong ba tab?

Cảm ơn

Trả lời

5

Đại biểu ứng dụng là nơi hợp lý để đặt. Một tùy chọn khác là tạo một lớp nhà máy singleton tùy chỉnh có một phương thức lớp trả về trình quản lý vị trí của bạn ủy nhiệm và thực hiện các phương thức ủy nhiệm ở đó. Điều đó sẽ giúp cho lớp ứng dụng của bạn trở nên sạch hơn.

Đây là triển khai lớp đơn lớp xương dựa trên số "Singletons in Cocoa: Doing them wrong" của Peter Hosey. Đây có thể là quá mức cần thiết, nhưng đó là một sự khởi đầu. Thêm phương thức đại biểu của bạn vào cuối.

static MyCLLocationManagerDelegate *sharedInstance = nil; 

+ (void)initialize { 
    if (sharedInstance == nil) 
     sharedInstance = [[self alloc] init]; 
} 

+ (id)sharedMyCLLocationManagerDelegate { 
    //Already set by +initialize. 
    return sharedInstance; 
} 

+ (id)allocWithZone:(NSZone*)zone { 
    //Usually already set by +initialize. 
    @synchronized(self) { 
     if (sharedInstance) { 
      //The caller expects to receive a new object, so implicitly retain it 
      //to balance out the eventual release message. 
      return [sharedInstance retain]; 
     } else { 
      //When not already set, +initialize is our caller. 
      //It's creating the shared instance, let this go through. 
      return [super allocWithZone:zone]; 
     } 
    } 
} 

- (id)init { 
    //If sharedInstance is nil, +initialize is our caller, so initialze the instance. 
    //If it is not nil, simply return the instance without re-initializing it. 
    if (sharedInstance == nil) { 
     if ((self = [super init])) { 
      //Initialize the instance here. 
     } 
    } 
    return self; 
} 

- (id)copyWithZone:(NSZone*)zone { 
    return self; 
} 
- (id)retain { 
    return self; 
} 
- (unsigned)retainCount { 
    return UINT_MAX; // denotes an object that cannot be released 
} 
- (void)release { 
    // do nothing 
} 
- (id)autorelease { 
    return self; 
} 
#pragma mark - 
#pragma mark CLLLocationManagerDelegateMethods go here... 
+0

Làm thế nào tôi sẽ đi về việc thực hiện một lớp nhà máy singleton? Nghe có vẻ như là một cách sạch sẽ, nhưng tôi không chắc chắn 100% bắt đầu từ đâu. – joec

+0

Xem ví dụ ở trên. –

+0

định dạng mã không hoạt động (xin lỗi!) – joec

1

Tôi chỉ cần bao gồm Trình quản lý vị trí của mình trong AppDelegate trực tiếp, vì nó đã thêm ít mã.

Tuy nhiên, nếu bạn định đưa Trình quản lý vị trí của mình vào AppDelegate, thì bạn nên cân nhắc sử dụng NSNotifications để cảnh báo các bộ điều khiển chế độ xem của bản cập nhật vị trí mà AppDelegate của bạn nhận được.

Xem liên kết này Send and receive messages through NSNotificationCenter in Objective-C?