Guest User

Untitled

a guest
Aug 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. How to passing class argument to selector in objective C?
  2. //Class Tile
  3.  
  4. @interface Tile : TouchableNode {
  5. NSString *val;
  6. }
  7.  
  8. -(void) setVal:(NSString *)v
  9. {
  10. val = v;
  11. }
  12.  
  13. -(NSString *) getVal
  14. {
  15. return val;
  16. }
  17.  
  18. for(Tile *tile in player)
  19. {
  20. if (tile.getVal == @"P") {
  21. if (pauseStatus == 0) {
  22. pauseStatus = 1;
  23.  
  24. [[CCDirector sharedDirector] pause];
  25. CGSize size = [[CCDirector sharedDirector] winSize];
  26. pauseLayer=[[CCLayer alloc] init];
  27. pauseLayer.anchorPoint=ccp(0,0);
  28. pauseLayer = [CCLayerColor layerWithColor: ccc4(0, 0, 255, 125) width: 300 height: 150];
  29. pauseLayer.position = ccp(size.width/2, size.height/2);
  30. pauseLayer.isRelativeAnchorPoint = YES;
  31. [self addChild: pauseLayer z:8];
  32.  
  33. //Here are 2 two button that when user click it will have @"+" value or @"-"
  34. plusBtn = [CCMenuItemImage itemFromNormalImage:@"plus.png" selectedImage:@"plus.png" target:self selector:@selector(onPlus:)];
  35. minusBTn = [CCMenuItemImage itemFromNormalImage:@"minus.png" selectedImage:@"minus.png" target:self selector:@selector(onMinus:)];
  36.  
  37. pauseMenu = [CCMenu menuWithItems:plusBtn, minusBTn, nil];
  38. [pauseMenu alignItemsHorizontally];
  39. [self addChild:pauseMenu z:10];
  40. }
  41. }
  42. }
  43.  
  44. -(void)onPlus:(Tile *) set
  45. {
  46. NSString *plus = @"+";
  47. [set setVal:plus];
  48. }
  49.  
  50. -(void)onMinus:(Tile *) set
  51. {
  52. NSString *minus = @"-";
  53. [set setVal:minus];
  54. }
  55.  
  56. plusBtn = [CCMenuItemImage itemFromNormalImage:@"plus.png" selectedImage:@"plus.png" target:self selector:@selector(onPlus:)];
  57. plusBtn.userData = (void*)tile; // You may need a bridge cast in ARC
  58. minusBTn = [CCMenuItemImage itemFromNormalImage:@"minus.png" selectedImage:@"minus.png" target:self selector:@selector(onMinus:)];
  59. minusBTn.userData = (void*)tile;
  60.  
  61. -(void)onPlus:(id)senderObj {
  62. CCNode *sender = (CCNode*)senderObj;
  63. Tile *myTile = (Tile*)sender.userData; // Again you may need a bridge cast here
  64. }
  65.  
  66. @interface MyCustomCCMenuItemImage : CustomCCMenuItemImage
  67. @property (nonatomic, retain) Tile* tile;
  68. @end
  69.  
  70. - (void)someMethod
  71. {
  72.  
  73. for(Tile *tile in player)
  74. {
  75. plusBtn = [MyCustomCCMenuItemImage itemFromNormalImage:@"plus.png" selectedImage:@"plus.png" target:self selector:@selector(onPlus:)];
  76. plusBtn.tile = tile ;
  77. }
  78.  
  79. }
  80.  
  81. //Your handler
  82. -(void)onPlus:(id)sender
  83. {
  84. Tile *myTile = sender.tile;
  85. }
  86.  
  87. [tile.getVal isEqualToString:@"P"]
Add Comment
Please, Sign In to add comment