El botón iOS creado mediante programación no funciona
Frecuentes
Visto 6,820 veces
4
Hola, tengo un 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");
}
5 Respuestas
2
I think labels recives touches instead of button. Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
contestado el 22 de mayo de 12 a las 16:05
Did that but no that doesn't work either :( Also set yes for userinteractionenabled for button. - CodeGek123
make sure that containerView and viewContainer have userInteractionEnabled = YES; - chico luna
YES i had set userinteraction enabled to container view but forgot to do it for viewcontainer. Setting that worked. Thanks - CodeGek123
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];
contestado el 22 de mayo de 12 a las 16:05
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 - de paso
I ran this code and it does work. Please give us more information if you want our help. - de paso
Eli, it works now. Reason being UserInteractionEnabled was not set to yes for two views i was using. - CodeGek123
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];
Esto es porque cuando llamas 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 aquí
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.
contestado el 23 de mayo de 12 a las 02:05
0
Add the colon in last of your method in selector
[connectedStories addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
respondido 15 mar '13, 10:03
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];
Respondido el 31 de diciembre de 13 a las 10:12
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas iphone objective-c ios ios4 uibutton or haz tu propia pregunta.
Is the button appearing as you want it? What type of view is contentView? Try calling
[contentView setUserEnabled:YES];
y mira si eso funciona - Dan Fwhat does [contentView userInteractionEnabled] and [viewContainer userInteractionEnabled] give? Maybe some of them are disabled and yout won't receive an event. - alex
Content view is just a UIVIEW. and apparently the above two methods i.e setUserEnabled or userInteractionEnabled are not declared in the interface :( - CodeGeek123
Actually setUserInteractionEnabled:YES is declared but no it doesnt work :( - CodeGeek123
Are the labels and backgroundImageView in or over the button, or are they separate things? In what class is the above code? - rdelmar