Cocos2d iphone 通过启用ARC的ccmenuitem传递用户数据

Cocos2d iphone 通过启用ARC的ccmenuitem传递用户数据,cocos2d-iphone,automatic-ref-counting,kobold2d,Cocos2d Iphone,Automatic Ref Counting,Kobold2d,我正在尝试使用以下代码通过menuitem传递NSString CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)]; buyButton.userData = (__bridge void *)((NSString*)(itemName)); 到以下选择器 -(void) buyItem:(CCMenuItemLabel*)sende

我正在尝试使用以下代码通过menuitem传递NSString

CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)];
buyButton.userData = (__bridge void *)((NSString*)(itemName));
到以下选择器

-(void) buyItem:(CCMenuItemLabel*)sender {
   NSString * itemName = (NSString *)sender.userData;
     }
但我在选择器中崩溃了。我使用的是启用了arc的cocos2d,因此在userdata分配中使用了桥接器。(kobold2d)。有什么想法吗?

试试这个,它很管用

 CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)];
 NSString *userDataString = [NSString stringWithFormat:@"kidnim"];            
 buyButton.userData = (__bridge void *)userDataString;

 CCMenu *menu = [CCMenu menuWithItems:buyButton, nil];
 menu.position = ccp(240, 160);
 [self addChild:menu];
和buyItem功能:

-(void) buyItem:(CCMenuItemLabel*)sender {
    NSString * itemName = (__bridge NSString*)sender.userData;
    printf("NSString: %s\n", [itemName UTF8String]);
}
你会被释放的

NSString:kidnim
试试这个,它很管用

 CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)];
 NSString *userDataString = [NSString stringWithFormat:@"kidnim"];            
 buyButton.userData = (__bridge void *)userDataString;

 CCMenu *menu = [CCMenu menuWithItems:buyButton, nil];
 menu.position = ccp(240, 160);
 [self addChild:menu];
和buyItem功能:

-(void) buyItem:(CCMenuItemLabel*)sender {
    NSString * itemName = (__bridge NSString*)sender.userData;
    printf("NSString: %s\n", [itemName UTF8String]);
}
你会被释放的

NSString:kidnim

您的实际碰撞问题是:

NSString * itemName = (NSString *)sender.userData;
看,你在这里投什么?右:您正在将
sender
转换为
NSString*
,然后向sender(CCMenuItemLabel)发送一条
userData
消息。砰

救援行动:

NSString * itemName = (__bridge NSString *)(sender.userData);

还有,当存在userObject时,为什么要让它变得过于复杂呢

buyButton.userObject = itemName;

userObject是
id
类型,不需要桥接,userData是
void*
并且需要桥接

您实际的崩溃问题是:

NSString * itemName = (NSString *)sender.userData;
看,你在这里投什么?右:您正在将
sender
转换为
NSString*
,然后向sender(CCMenuItemLabel)发送一条
userData
消息。砰

救援行动:

NSString * itemName = (__bridge NSString *)(sender.userData);

还有,当存在userObject时,为什么要让它变得过于复杂呢

buyButton.userObject = itemName;

userObject是
id
类型,不需要桥接,userData是
void*
并且需要桥接

您的代码确实有效,谢谢,但不幸的是我需要设置字符串。我不明白为什么您的代码工作得很好,但将userdata定义更改为:'buyButton.userdata=(uu桥空*)((NSString*)([NSString stringWithFormat:@“Dendim”]);'幸运的是,当前发布的代码不适用于我。当代码单独在一个层上运行时,它可以工作,但在其他一些代码的下面,它会退出。另一个代码是从plist文件加载信息并创建CCScrollLayer。删除一个以这一行开头的块似乎可以使它工作:`if(![[NSFileManager defaultManager]fileExistsAtPath:plistPath]){plistPath=[[NSBundle mainBundle]pathForResource:FileOfType:@“plist”];}`但是我使用的是用户对象答案,谢谢你的代码确实工作了,谢谢,但不幸的是我需要设置字符串。我不明白为什么您的代码工作得很好,但将userdata定义更改为:'buyButton.userdata=(uu桥空*)((NSString*)([NSString stringWithFormat:@“Dendim”]);'幸运的是,当前发布的代码不适用于我。当代码单独在一个层上运行时,它可以工作,但在其他一些代码的下面,它会退出。另一个代码是从plist文件加载信息并创建CCScrollLayer。删除一个以这一行开头的块似乎可以使它正常工作:`if(![[NSFileManager defaultManager]fileExistsAtPath:plistPath]){plistPath=[[NSBundle mainBundle]pathForResource:FileOfType:@“plist”];}`但我使用的是用户对象回答thanksthanks steffen,我没有尝试你提到的正确的演员阵容,但是userobject是正确的动作,效果很好!看看你用kobold套件做了什么!谢谢steffen,我没有尝试你提到的合适的演员阵容,但是userobject是正确的动作,效果很好!看看你用kobold套件做了什么!