How can I connect the IBAction method to a simple view for a touch event?

In Xcode, I created an empty new views-based application project. He generated myProjectViewController and a tip for it. There is only one view for this view controller in this tip. I wanted to test the event handling material and created a method - (IBAction) that would simply register the “hello world” when I touch the view. But for some reason IB does not give me the opportunity to connect an action. What am I doing wrong there? I also tried setting UIView as a subroutine. When I drag it from File Owner (the whoose class is myProjectViewController, where I have IBAction in the header), IBAction is not even mentioned. But really should it be right?

+3
source share
6 answers

There is an easy way to achieve this. Go to your interface builder of your class -> Choose u view to add an action to -> then change your custom class from IDENTITY INSPECTOR from UIView to "UIControl". Now you can add any IBAction method to this view.

Change class to UIControl

add IBAction event to this view to perform action method

+9
source

IBAction is just a tag that you add to a method declaration that identifies this method as a candidate for connecting to a control action.

The IBAction method is a method that receives a message about the action of some other control.

UIViews . UIControls. , , UIView . IBActions UIControl UIBarButtonItems.

+5

Began, touchsMoved touchesEnded .

+3

IBAction - , . Interface Builder, ( ).

UIView, . . /.

0

I think you can use the touchhesBegan or touchesEnded method, as @lostInTransit suggested. Its the right way instead of making it look like a button view.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

// do something here

}
0
source

Make your view a UIButton class. This will make the view crocheted.

-1
source

Source: https://habr.com/ru/post/1709086/


All Articles