找到你要的答案

Q:ios How to know which UIView when I click from xib?

Q:iOS如何知道当我点击UIView XIB?

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.

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.

当我编写的代码,它会得到正确的结果表明两UIViews。

xibview m文件部分代码如下:

 -(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");
      ......
 }

但我怎么知道哪个是我的UIView的点击?

我添加一些细节描述。

XIB文件中自定义类,我将使用后的通知的处理和一些数据时,用户点击xib(touchesBegan是XIB的自定义类)。

我有两个UIView的故事情节,这些UIView会使用XIB文件。

所以我想知道,当我点击它UIView,我可以知道哪一个用户点击。

/ / -------------回答。请@ Boyi Li回答。---------

在xibview。米有加

[超级touchesBegan:触摸事件:事件];

重写触动开始的方法。

在xibview Controller添加头文件。

它可以拖动指视图。

answer1: 回答1:

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
     }
 }

正如您所说,您可以为这两个视图分配不同的标记。在XibView,你可以检查self.tag = = & lt;标签& gt;。

如果你想在父视图或控制器,得到特定的视图中使用viewwithtag:方法。它将检查标签属性与标签参数中的值匹配的层次结构。

更新:

创建的左和右视图ViewController IBOutlet参考。在你的控制器中,你有:

@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
     }
 }
answer2: 回答2:
- (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
   {

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

    [超级touchesBegan:触摸事件:事件];
    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
   {

   }
 }
ios  objective-c  xcode  uiview  xib