问题是,对象A试图在对象B获得核心位置数据之前使用这些空变量。对象A继续运行,对象B的核心位置数据还不可用。
对象A如何有效地“等待”,或者被告知数据已经存在并可以继续进行?
谢谢,里克
发布于 2011-04-17 01:41:51
简单的方法,NSNotificationCenter。
在对象B头文件中:
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]);
}https://stackoverflow.com/questions/5690764
复制相似问题