سؤال

لدي تطبيق مع شريط تبويب و 3 علامات تبويب. ستكون هناك حاجة إلى الموقع الحالي للمستخدم معروفا في أي من علامات التبويب الثلاثة. سيكون أفضل مكان لتنفيذه CLLocationManager كن في مندوب التطبيق في هذه الحالة؟

هل هو موافق (ممارسة جيدة؟) وضع طرق مندوب CllocationManager في تطبيق منفد م؟

أين تقترح أن أضع CLLocationManager كما سأكون الاتصال -startUpdatingLocation من أي من علامات التبويب الثلاثة؟

شكرا

هل كانت مفيدة؟

المحلول

مندوب التطبيق هو مكان معقول لوضعه. سيكون هناك خيار آخر هو إنشاء فئة مصنع مخصصة Singleton لديها طريقة فئة ترجع مندوب مدير موقعك وتنفيذ أساليب المندوب هناك. من شأنها أن تبقي تطبيقك المنفث منظف الطبقة.

إليك منفذ فئة Singleton هيكل عظمي مقدم من بيتر هوسي "المفردات في الكاكاو: يفعلونهم خطأ". وبعد قد يكون هذا مبالغا فيه، لكنها بداية. أضف أساليب المفوض الخاصة بك في النهاية.

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...

نصائح أخرى

لقد قمت ببساطة بتضمين موقعي MyManager في بلدي AppDelegate مباشرة، لأنه أضاف رمز صغير.

ومع ذلك، إذا كنت ستقوم بتضمين موقعك مانياغر في AppDelegate، فعليك التفكير في استخدام NSNotifications لتنبيه ViewControllers الخاص بك في الموقع تحديثات appdelegate تلقيها.

انظر هذا الرابطإرسال واستقبال الرسائل من خلال NSNotificationCenter في الهدف-ج؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top