需要發送CGpoint 到主類中。使用了Notification,但是不知在接受端如何從Notification中提取出CGpoint。在log輸出的結果是0.0
代碼
發送:
NSValue *pointAsObject =[NSValue valueWithCGPoint:CGPointMake(touchDetectingView.lastTouchPosition.x, touchDetectingView.lastTouchPosition.y)];
[[NSNotificationCenter defaultCenter] postNotificationName: @"swap" object:pointAsObject];
接收端 Header 文件:
- (void)incomingNotification:(NSNotification *)notification;
接受端 M 文件:
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"swap" object:nil];
- (void)incomingNotification:(NSNotification *)notification
{
NSValue *pointAsObject = [[notification userInfo] valueForKey:@"swap"];
NSLog (@"Successfully received the test notification %f",pointAsObject.CGPointValue.y);
}
請看黑體字,你應該放到userinfo裡傳遞,post的時候,而不是通過object傳遞
NSValue *pointAsObject =[NSValue valueWithCGPoint:CGPointMake(touchDetectingView.lastTouchPosition.x, touchDetectingView.lastTouchPosition.y)];
NSDictionary *dict = [NSDictionary dictionaryWithObject:pointAsObject
forKey:@"point"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"swap" object:self userinfo:dict];* **
接收並處理消息
- (void)incomingNotification:(NSNotification *)notification
{
NSValue *pointAsObject = [[notification userInfo] valueForKey:@"point"];
NSLog (@"Successfully received the test notification %f",pointAsObject.CGPointValue.y);
}