0
votes

Because some reasons, I create a UIView xib file to reuse.
How to know which UIView when I click from xib?

I create a xib file extends UIView(named with XibView).
Then I drag two UIView(leftView,rightView) in the storyboard, and set the custom class "XibView" in XCode inspector window.

When I compile the code, It will get correct result that show two UIViews.

XibView.m file part code below:

 -(id) initWithCoder:(NSCoder *)aDecoder
 {
     self = [super initWithCoder:aDecoder];

     if( self )
     {

          UIView *containerView = [[[UINib nibWithNibName:@"XibView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
          CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
          containerView.frame = newFrame;
          [self addSubview:containerView];
     }
    return self;
 }

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
      NSLog(@"tap in xib");
      ......
 }

But How can I know which UIView is my click?

// I add some detail description.

In the xib custom class, I will using post the notify to the uiviewcontroller and take some data when user click in xib(touchesBegan is in the xib custom class).

I have two uiview in the storyboard , these uiview will using the xib file.

so I want to know when I click which uiview, I can know which one user click.

// ------------- answer . please refer @Boyi Li answer. ---------

In the XibView.m had add the

[super touchesBegan:touches withEvent:event];

to override the touches begain method.

and add the XibView header file in the viewcontroller.

It can drag the refer to the viewcontroller.

2
assign tag to every view.. from it you can easily get which view you have clicked.Ashok Londhe
Hi Ashok Londhe, I have this idea to set the tag, but I found if I set the view to custom class in inspector window, I cant set the instant tag value,how can I set the instant and load the xib file, that can set the tag using the new xib instance.dickfala
I had add more description in the question.dickfala

2 Answers

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

    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    if ([touch.view isKindOfClass: UIView.class])
    {
      UIView *view=touch.view;
      if (view==YourView1)
      {
        //start editing

      }else
      if (view==YourView2)
      {
        //start editing

      }
   }
   else
   {

   }
 }
0
votes

As your said, you could assign different tag for these two views. Inside XibView, you could check self.tag == <tag>.

If you want to get specific view in parent view or controller, use viewWithTag: method. It will check hierarchy whose tag property matches the value in the tag parameter.

Update:

Create IBOutlet Reference in ViewController for left and right Views. In your controller, you have:

@property (nonatomic, weak) IBOutlet XibView *leftView;
@property (nonatomic, weak) IBOutlet XibView *rightView;`

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint leftTouchPoint = [touch locationInView:leftView];
     CGPoint rightTouchPoint = [touch locationInView:rightView];
     if ([self.leftView pointInside:leftTouchPoint withEvent:event]) {
         // Do something for leftView
     } else if ([self.rightView pointInside:rightTouchPoint withEvent:event]) {
         // Do something for Right View
     }
 }