Je sais comment ajouter une action IBAction à un bouton en le faisant glisser depuis le constructeur d'interface, mais je souhaite ajouter l'action par programme pour gagner du temps et éviter de basculer constamment. La solution est probablement très simple, mais je n'arrive pas à trouver de réponse lorsque je la cherche. Je vous remercie!
Essaye ça:
Swift 4
myButton.addTarget(self,
action: #selector(myAction),
for: .touchUpInside)
Objective-C
[myButton addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
Vous pouvez trouver une riche source d'informations dans la documentation Apple. Jetez un coup d'œil à la documentation d'UIButton , elle révélera qu'UIButton est un descendant de IControl, qui implémente la méthode à ajouter des cibles .
-
Vous devrez faire attention à l'ajout ou non de deux-points après myAction
dans action:@selector(myAction)
Voici le référence .
Réponse rapide:
myButton.addTarget(self, action: "click:", for: .touchUpInside)
func click(sender: UIButton) {
print("click")
}
Documentation: https://developer.Apple.com/documentation/uikit/uicontrol/1618259-addtarget
CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[view addSubview:button];
CGRect buttonFrame = CGRectMake( x-pos, y-pos, width, height ); //
CGRectMake(10,5,10,10)
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor BlueVolor] forState:
UIControlStateNormal];
[view addSubview:button];
-(void)btnClicked {
// your code }
essaye ça:
commencez par écrire ceci dans votre fichier .h de viewcontroller
UIButton *btn;
Maintenant, écrivez ceci dans votre fichier .m de viewcontrollers viewDidLoad.
btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[btn setBackgroundColor:[UIColor orangeColor]];
//adding action programatically
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
écrire cette méthode viewDidLoad extérieure dans le fichier .m de votre contrôleur de vue
- (IBAction)btnClicked:(id)sender
{
//Write a code you want to execute on buttons click event
}
Pour Swift
Créez d'abord une fonction pour l'action du bouton, puis ajoutez-la à votre cible de bouton
func buttonAction(sender: UIButton!) {
print("Button tapped")
}
button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)
UIButton *btnNotification=[UIButton buttonWithType:UIButtonTypeCustom];
btnNotification.frame=CGRectMake(130,80,120,40);
btnNotification.backgroundColor=[UIColor greenColor];
[btnNotification setTitle:@"create Notification" forState:UIControlStateNormal];
[btnNotification addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnNotification];
}
-(void)btnClicked
{
// Here You can write functionality
}
UIButton
hérite de UIControl
. Cela a toutes les méthodes pour ajouter/supprimer des actions: Référence de la classe UIControl . Consultez la section "Préparation et envoi de messages d'action", qui couvre – sendAction:to:forEvent:
et – addTarget:action:forControlEvents:
.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];