Cocoa touch 对象A如何等待对象B在其核心位置委托中完成位置数据的获取,以便使用该数据? 对象A调用对象B以启动核心位置并获取位置数据 对象B也是核心位置委托,接收到成功的回调,即接收到数据
对象B的委托停止核心位置,因为只需要一个位置数据点Cocoa touch 对象A如何等待对象B在其核心位置委托中完成位置数据的获取,以便使用该数据? 对象A调用对象B以启动核心位置并获取位置数据 对象B也是核心位置委托,接收到成功的回调,即接收到数据,cocoa-touch,ios,core-location,Cocoa Touch,Ios,Core Location,对象B的委托停止核心位置,因为只需要一个位置数据点 对象A希望使用该位置数据,并且确实可以访问对象B中的位置变量 问题是对象A试图在对象B获取核心位置数据之前使用这些空变量。对象A在上比赛,对象B的核心位置数据尚不可用 对象是如何有效地“等待”的,或是如何得到数据已经存在并且可以继续的通知的 谢谢,Ric简单的方法 在对象B头文件中: extern NSString *const kLocationKey; extern NSString *const kIGotLocationNotific
extern NSString *const kLocationKey;
extern NSString *const kIGotLocationNotification; // whatever name you like
在对象B实现文件中:
// 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实现文件中:
// 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]);
}
如果对象A的代码是视图控制器,则使用NSNotificationCenter的想法是可行的,因为它位于系统运行循环中。实际上,我已经将对象A的代码放在应用程序委托的ApplicationIDFinishLaunching中,但问题是它没有停止,而是继续运行到方法的末尾。没有机会等待通知。我得重新考虑对象A的代码是从哪里来的。你把我弄丢了。运行end的方法是什么?发布您在问题中所讨论的方法。想要使用Loc数据的代码在AppDidFinishLaunching中。该代码方法不等待任何人,它将运行到(方法的)末尾。没有办法停止并等待loc数据在那里可用。我必须重新考虑这个核心位置代码的请求。不,你不想在AppDidFinishLaunching中等待。就像你说的,你必须重新考虑你的核心位置代码。也许用一个标志来表示你有位置。任何需要位置的代码都可以检查标志是否正确。