4

Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.

UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor]; 
[connectedStories setTitle:@"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];

[viewContainer addSubview:contentView];
return contentView;

- (void)buttonClicked
{
  NSLog(@"button clicked");
}
12
  • Is the button appearing as you want it? What type of view is contentView? Try calling [contentView setUserEnabled:YES]; and see if that works Commented May 22, 2012 at 15:26
  • 2
    what does [contentView userInteractionEnabled] and [viewContainer userInteractionEnabled] give? Maybe some of them are disabled and yout won't receive an event. Commented May 22, 2012 at 15:26
  • Content view is just a UIVIEW. and apparently the above two methods i.e setUserEnabled or userInteractionEnabled are not declared in the interface :( Commented May 22, 2012 at 15:32
  • Actually setUserInteractionEnabled:YES is declared but no it doesnt work :( Commented May 22, 2012 at 15:33
  • Are the labels and backgroundImageView in or over the button, or are they separate things? In what class is the above code? Commented May 22, 2012 at 15:39

5 Answers 5

2

I think labels recives touches instead of button. Add userInteractionEnabled = NO; to your labels.

label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Sign up to request clarification or add additional context in comments.

3 Comments

Did that but no that doesn't work either :( Also set yes for userinteractionenabled for button.
make sure that containerView and viewContainer have userInteractionEnabled = YES;
YES i had set userinteraction enabled to container view but forgot to do it for viewcontainer. Setting that worked. Thanks
0

Try changing the top part of your code to this:

CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];

//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]]; 
[connectedStories setTitle:@"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];

3 Comments

And make sure the labels being added to the view are not within the same bounds as the button, otherwise they may be covering over the button
I ran this code and it does work. Please give us more information if you want our help.
Eli, it works now. Reason being UserInteractionEnabled was not set to yes for two views i was using.
0

It may very well be the following:

[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];

im sure you would need to add them in the following order:

[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];

This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.

Check out the UIView Documentation here

the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.

By default the last subView captures all touch events and doesn't pass them on.

Comments

0

Add the colon in last of your method in selector

[connectedStories addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [label setText:storyTitle];

Comments

0
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
                 action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f  alpha:1];
 [custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
 [view addSubview:custombutton];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.