ghostrider
ghostrider

Reputation: 5259

button of a dynamically created view does not respond

I need your help. I am trying to build a game. When a hint button is pressed a new view is created programmatically with the code below:

In my gameController.m

//connect the Hint button
-(void)setHud:(HUDView *)hud
{
    _hud = hud;
    [hud.btnHelp addTarget:self action:@selector(actionHint) forControlEvents:UIControlEventTouchUpInside];
}

//the user pressed the hint button
-(void)actionHint
{
    CGRect  viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
    HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
    [self.gameView addSubview:hintMenu];
}

This is my new View created programmatically.

+(instancetype)viewWithRect:(CGRect)r
{
    HintMenu* hint = [[HintMenu alloc] initWithFrame:r];
    hint.userInteractionEnabled = YES;

    UIImage* image=[UIImage imageNamed:@"btn"];

    hint.btnHelp = [UIButton buttonWithType:UIButtonTypeCustom];
    [hint.btnHelp setTitle:@"button" forState:UIControlStateNormal];
    hint.btnHelp.titleLabel.font = kFontHUD;
    [hint.btnHelp setBackgroundImage:image forState:UIControlStateNormal];
    hint.btnHelp.frame = CGRectMake(50, 30, image.size.width, image.size.height);
    hint.btnHelp.alpha = 0.8;
    [hint addSubview: hint.btnHelp];

    UIImage* image2=[UIImage imageNamed:@"tile"];
    hint.imageHelp=[[UIImageView alloc] initWithFrame:CGRectMake(image.size.width + 50 + 10, 30, image2.size.width, image2.size.height/2)];
    hint.imageHelp.image=image2;
    [hint addSubview:hint.imageHelp];



    return hint;
}

I want to identify when the button in the new view is being pressed. Similarly with the above I add in my GameController.m

//connect the button in the Menu
-(void)setHintMenu:(HintMenu *)hintMenu
{
    _hintMenu = hintMenu;
    [hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
}

//the user pressed the menu button
-(void)actionHintMenu
{
    NSLog(@"Button in menu pressed");
}

but the log is not shown. Can you help me on this?

edit: If this code is added in the HintMenu.m which is my new Created view like this:

+(instancetype)viewWithRect:(CGRect)r
{
...
hint.btnHelp.alpha = 0.8;
    [hint.btnHelp addTarget:self action:@selector(actionHintMenu:) forControlEvents:UIControlEventTouchUpInside];
    [hint addSubview: hint.btnHelp];
..
}
//the user pressed the menu button
-(void)actionHintMenu:(UIButton *) button
{
    NSLog(@"Button in menu pressed");
}

I am getting this error:

2013-06-16 16:44:30.559  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[HintMenu actionHintMenu]: unrecognized selector sent to class 0xe6f8'
*** First throw call stack:

Upvotes: 0

Views: 86

Answers (2)

Adithya
Adithya

Reputation: 4705

Answer to you edited question

Replace

[hint.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];

with

[hint.btnHelp addTarget:hint action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

Adithya
Adithya

Reputation: 4705

I am not sure why you have added [hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside]; inside the setter method of your hintMenu view.

There is no call for the setter method of hintMenu and so the button target-action is not getting set.

I would suggest remove the -(void)setHintMenu:(HintMenu *)hintMenu completely. Update actionHint

-(void)actionHint
{
   CGRect  viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
   HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
   [hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
   [self.gameView addSubview:hintMenu];
}

This is just a fix. If you ask me the right method, I would suggest setting the button target should be done within your custom view itself. For that you might maintain a delegate property in the view.

Upvotes: 1

Related Questions