ios xib点击时如何知道是哪个UIView?
ios How to know which UIView when I click from xib?
因为一些原因,我创建了一个UIView xib文件来重用。
xib点击时如何知道是哪个UIView?
我创建了一个扩展 UIView 的 xib 文件(以 XibView 命名)。
然后我在故事板中拖动两个 UIView(leftView,rightView),并在 XCode inspector window.
中设置自定义 class "XibView"
当我编译代码时,它会得到显示两个 UIView 的正确结果。
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 自定义 class 中,我将使用 post 通知 uiviewcontroller 并在用户点击 xib 时获取一些数据(touchesBegan 在 xib 自定义中 class).
我在故事板中有两个uiview,这些uiview将使用xib文件。
所以我想知道当我点击哪个uiview时,我可以知道哪个用户点击了。
// ------------- 回答。请参考@Boyi Li 的回答。 --------
在XibView.m中添加了
[超级touchesBegan:toucheswithEvent:event];
覆盖 touches begain 方法。
并在viewcontroller.
中添加XibView头文件
可以拖动引用viewcontroller。
如您所说,您可以为这两个视图分配不同的标签。在 XibView
中,您可以检查 self.tag == <tag>
.
如果要在父视图或控制器中获取特定视图,请使用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
}
}
- (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
{
}
}
因为一些原因,我创建了一个UIView xib文件来重用。
xib点击时如何知道是哪个UIView?
我创建了一个扩展 UIView 的 xib 文件(以 XibView 命名)。
然后我在故事板中拖动两个 UIView(leftView,rightView),并在 XCode inspector window.
当我编译代码时,它会得到显示两个 UIView 的正确结果。
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 自定义 class 中,我将使用 post 通知 uiviewcontroller 并在用户点击 xib 时获取一些数据(touchesBegan 在 xib 自定义中 class).
我在故事板中有两个uiview,这些uiview将使用xib文件。
所以我想知道当我点击哪个uiview时,我可以知道哪个用户点击了。
// ------------- 回答。请参考@Boyi Li 的回答。 --------
在XibView.m中添加了
[超级touchesBegan:toucheswithEvent:event];
覆盖 touches begain 方法。
并在viewcontroller.
中添加XibView头文件可以拖动引用viewcontroller。
如您所说,您可以为这两个视图分配不同的标签。在 XibView
中,您可以检查 self.tag == <tag>
.
如果要在父视图或控制器中获取特定视图,请使用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
}
}
- (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
{
}
}