程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> cgpoint-ios中傳遞CGpoint 結果錯誤

cgpoint-ios中傳遞CGpoint 結果錯誤

編輯:編程綜合問答
ios中傳遞CGpoint 結果錯誤

需要發送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);
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved