Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Objective-C</strong></p> <pre><code>UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect]; [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [but setFrame:CGRectMake(52, 252, 215, 40)]; [but setTitle:@"Login" forState:UIControlStateNormal]; [but setExclusiveTouch:YES]; // if you like to add backgroundImage else no need [but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal]; [self.view addSubview:but]; -(void) buttonClicked:(UIButton*)sender { NSLog(@"you clicked on button %@", sender.tag); } </code></pre> <p><strong>Swift</strong></p> <pre><code> let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom) myButton.setTitle("Hai Touch Me", forState: .Normal) myButton.setTitleColor(UIColor.blueColor(), forState: .Normal) myButton.frame = CGRectMake(15, 50, 300, 500) myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside) self.view.addSubview( myButton) func pressedAction(sender: UIButton!) { // do your stuff here NSLog("you clicked on button %@", sender.tag) } </code></pre> <p><strong>Swift3 and above</strong></p> <pre><code> let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom) myButton.setTitle("Hi, Click me", for: .normal) myButton.setTitleColor(UIColor.blue, for: .normal) myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500) myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside) self.view.addSubview( myButton) func pressedAction(_ sender: UIButton) { // do your stuff here print("you clicked on button \(sender.tag)") } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload