首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对象A如何等待对象B在其核心位置代表中完成获取位置数据,以便能够使用这些数据?

对象A如何等待对象B在其核心位置代表中完成获取位置数据,以便能够使用这些数据?
EN

Stack Overflow用户
提问于 2011-04-17 01:05:29
回答 1查看 196关注 0票数 0
  • 对象A调用对象B启动核心位置并获取位置数据。
  • 对象B也是核心位置代表,并接收一个成功的回调,数据被接收。
  • 对象B的委托停止核心位置,因为只需要一个位置数据点。
  • 对象A希望使用该位置数据,并且可以访问对象B中的位置变量。

问题是,对象A试图在对象B获得核心位置数据之前使用这些空变量。对象A继续运行,对象B的核心位置数据还不可用。

对象A如何有效地“等待”,或者被告知数据已经存在并可以继续进行?

谢谢,里克

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-17 01:41:51

简单的方法,NSNotificationCenter

在对象B头文件中:

代码语言:javascript
复制
extern NSString *const kLocationKey;
extern NSString *const kIGotLocationNotification; // whatever name you like

在对象B实现文件中:

代码语言:javascript
复制
// assign a string we will use for the notification center
NSString *const kIGotLocationNotification = @"Any text you like here";
NSString *const kLocationKey = @"Location";


// in the method where you stop core location
CLLocation *loc;

// create a dictionary object with the location info
NSDictionary *dict = [NSDictionary dictionaryWithObject:loc forKey:kLocationKey];

// you post a notification to the default center
[[NSNotificationCenter defaultCenter] postNotificationName:kIGotLocationNotification object:self userInfo:dict];

在对象A实现文件中:

代码语言:javascript
复制
// inside your init method 
// become an observer with the default center
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIGotLocation:) name:kIGotLocationNotification object:nil];

// inside your dealloc
// don't forget to remove yourself from the notification default center
[[NSNotificationCenter defaultCenter] removeObserver:self];


// create the selector that will receive the notification
- (void)handleIGotLocation:(NSNotification *)pNotification {
    NSLog(@"Name: %@", [pNotification name]);
    NSLog(@"Object: %@", [pNotification object]);

    // the user info is going to contain the dict with your location
    NSLog(@"UserInfo: %@", [pNotification userInfo]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5690764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档