Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create IBOutlets and IBActions programmatically [duplicate]

I want to create IBOutlets and IBActions 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?

like image 467
user3705416 Avatar asked Jun 04 '14 04:06

user3705416


1 Answers

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 {
}
like image 58
danh Avatar answered Oct 23 '22 13:10

danh