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");
}
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
CodeGeek123
  • 4,341
  • 8
  • 50
  • 79
  • Is the button appearing as you want it? What type of view is contentView? Try calling `[contentView setUserEnabled:YES];` and see if that works – Dan F May 22 '12 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. – alex May 22 '12 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 :( – CodeGeek123 May 22 '12 at 15:32
  • Actually setUserInteractionEnabled:YES is declared but no it doesnt work :( – CodeGeek123 May 22 '12 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? – rdelmar May 22 '12 at 15:39
  • The button appears over the backgroundImageView. the lables and buttons are slightly separated but sits on top of thebackgroundimageview – CodeGeek123 May 22 '12 at 15:41
  • Try a different initializing method. UIButton *button = [[UIButton alloc] initWithFrame:frame]; – esreli May 22 '12 at 15:42
  • also, try changing the target:self to target:nil – esreli May 22 '12 at 15:42
  • InitiwithFrame does not work. – CodeGeek123 May 22 '12 at 15:49
  • Target to nil also does not work :( – CodeGeek123 May 22 '12 at 15:49
  • I ran the code in the answer I posted below and it does work. You are not giving us enough information to correctly diagnose the problem. – esreli May 22 '12 at 16:09
  • Hi Eli, thank you for the response. My error was caused because my UserInteractionEnabled was not set to yes. Once i set it, it was ok. – CodeGeek123 May 23 '12 at 10:34

5 Answers5

2

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

label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Moonkid
  • 881
  • 7
  • 17
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];
esreli
  • 4,993
  • 2
  • 26
  • 40
  • 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 – esreli May 22 '12 at 15:57
  • I ran this code and it does work. Please give us more information if you want our help. – esreli May 22 '12 at 16:09
  • Eli, it works now. Reason being UserInteractionEnabled was not set to yes for two views i was using. – CodeGeek123 May 23 '12 at 10:35
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.

CStreel
  • 2,642
  • 2
  • 19
  • 37
0

Add the colon in last of your method in selector

[connectedStories addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [label setText:storyTitle];
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
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];