1

I have a xib file which I created using New -> iOS -> User Interface -> View. Then to inflate/load it into my UIViewController I do

UIView *myView = [[UINib nibWithNibName:@"FileName" bundle:nil] instantiateWithOwner:self options:nil].firstObject;

Now that I have the view, I need to know when the user taps on it. How might I set such IBAction (or whatever the appropriate term)?

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • You didn't use Storyboard ? – Raptor Oct 16 '14 at 08:16
  • This is just a view, not a UIViewController. And this is a view that I will dynamically show/hide in UIViewController. So no I didn't use storyboard for that view. But the UIViewController was specified using storyboard. – Katedral Pillon Oct 16 '14 at 08:21
  • If the question seems shallow that's because until now I have always exclusively used the storyboard. This is the first time I am using xib. And I don't know how to get an IBAction connected to the view in the xib. – Katedral Pillon Oct 16 '14 at 08:26

2 Answers2

0

Always you set the IBAction in the ViewContoller , so bind the action to the xib by specify the file owner is the view controller. You also can dynamic add view touch handle by use addGestureRecognizer to handle the event on the view. exampe in : add gesture to the view

Community
  • 1
  • 1
Feng Lin
  • 670
  • 4
  • 8
  • I remember Gesture as complicated. So that's why I just wanted to have an IBAction method. Again, this is a View not a UIViewController. I don't know how to manipulate the xib file to point to an IBAction – Katedral Pillon Oct 16 '14 at 08:22
  • You can see the example:http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/ – Feng Lin Oct 16 '14 at 08:35
0

There is the Owner element in XIBs. AFAIK, only the owner can be used in manual load scenarios.

In the XIB editor, link events to the owner element. When instantiate the XIB, make the owner parameter object provide required event handlers.

9dan
  • 4,222
  • 2
  • 29
  • 44
  • by owner do you mean `instantiateWithOwner:`? Or are you referring to a field in "Interface Builder" inspectors? – Katedral Pillon Oct 16 '14 at 08:25
  • I mean, with `instantiateWithOwner:` method, we can provide the owner object on loading. Of course, this owner is the "Interface Builder" inspector Owner. – 9dan Oct 16 '14 at 08:39