I want to create IBOutlet
s and IBAction
s in code rather than with the interface builder.
Let's say I've got a button on my UI called btnDisplay
, and a method in my code called displayMessage
. What would be the code I'd have to write to make it so that when btnDisplay
is tapped, displayMessage
runs?
The way to do that with no outlets would be to give the button a tag in IB, say 128. Then:
UIButton *btnDisplay = (UIButton *)[self.view viewWithTag:128];
[btnDisplay addTarget:self action:@selector(pressedBtnDisplay:) forControlEvents:UIControlEventTouchUpInside];
Then implement:
- (void) pressedBtnDisplay:(id)sender {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With