IOS soft button not working

Hi I have UIButtonone that I created programmatically, but unfortunately it does not work. When I click on UIButton, nothing happens. I do not know what the problem is. 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");
}
+3
source share
5 answers

I think labels will get touches instead of buttons. Add userInteractionEnabled = NO; to your tags.

label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
+2
source

Try changing the top of your code to the following:

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];
0
source

:

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

, :

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

, , addSubview:, , .

UIView

the reason the interaction is not detected is because you do not pass touchEvents through other sub-objects as they are captured by shortcut1 and stopped there.

By default, the last subviewer captures all touch events and does not transmit them.

0
source

Add a colon to the last of your method in the selector

[connectedStories addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [label setText:storyTitle];
0
source
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];
0
source

All Articles