Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hook up an IBAction method to a plain view for a touch up event?

I have created a blank new view-based application project in Xcode. It generated a myProjectViewController and an nib for it. In that nib for that view controller, there is just one view. I wanted to test some event handling stuff and created an -(IBAction) method that will just log a "hello world" when I touch the view. But for some reason, IB doesn't give me a chance to hook up the action. What am I doing wrong there? I also tried to put a UIView as subview there. When I drag from that to File's Owner (whoose class is the myProjectViewController, where I have the IBAction in the header), doesn't even mention the IBAction. But it actually should, right?

like image 404
Thanks Avatar asked May 26 '09 11:05

Thanks


People also ask

What is the difference between IBOutlet and IBAction?

An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.

What is@ IBAction swift?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.

What does IBAction stand for?

"Interface Builder". Before Xcode 4, the interface files (XIBs and NIBs) were edited in a separate program called Interface Builder, hence the prefix. IBAction is defined to void , and IBOutlet to nothing. They are just clues to Interface Builder when parsing files to make them available for connections.


2 Answers

There is an easy way to achieve this. Goto your Interface builder of your class >> Select the view u want to add action to >> then change its custom class from IDENTITY INSPECTOR from UIView to 'UIControl'. Now u can add any IBAction method to this view.

Change class to UIControl

add IBAction event to this view to perform action method

like image 92
Nishant Avatar answered Nov 03 '22 08:11

Nishant


IBAction is just a tag that you add to a method declaration that identifies that method as a candidate for being connected to a control's action.

An IBAction method is the method that receives the action message of some other control.

UIViews don't send any actions. UIControls do. So there's nothing to hook up from a plain UIView to your object. You can only hook up IBActions to UIControl subclasses and UIBarButtonItems.

like image 36
Jon Hess Avatar answered Nov 03 '22 08:11

Jon Hess