ios如何知道从xib单击时查看哪个UIView?
由于某些原因,我创建了一个UIView xib文件以供重用。ios如何知道从xib单击时查看哪个UIView?,ios,objective-c,xcode,uiview,xib,Ios,Objective C,Xcode,Uiview,Xib,由于某些原因,我创建了一个UIView xib文件以供重用。 当我从xib单击时,如何知道哪个UIView 我创建了一个扩展UIView的xib文件(以XibView命名)。 然后我在情节提要中拖动两个UIView(leftView,rightView),并在XCode inspector窗口中设置自定义类“XibView” 当我编译代码时,它将得到显示两个UIView的正确结果 XibView.m文件部件代码如下: -(id) initWithCoder:(NSCoder *)aDecode
当我从xib单击时,如何知道哪个UIView 我创建了一个扩展UIView的xib文件(以XibView命名)。
然后我在情节提要中拖动两个UIView(leftView,rightView),并在XCode inspector窗口中设置自定义类“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自定义类中,我将使用post将通知发送到uiviewcontroller,并在用户单击xib时获取一些数据(touchesStart在xib自定义类中)
我在故事板中有两个uiview,这些uiview将使用xib文件
所以我想知道当我点击哪个uiview时,我可以知道哪个用户点击
//------回答。请参考@Boyi Li的答案---------
在XibView.m中,添加了
[超级触摸开始:触摸事件:事件]
要覆盖touchs begain方法
并将视图头文件添加到viewcontroller中
它可以将引用拖动到viewcontroller。如您所说,您可以为这两个视图指定不同的标记。在
XibView
中,您可以检查self.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
}
}
为每个视图指定标记。。从它你可以很容易地得到你点击的视图。你好,Ashok Londhe,我有这个想法来设置标签,但是我发现如果我在inspector窗口中将视图设置为custom class,我无法设置即时标签值,我如何设置即时并加载xib文件,这可以使用新的xib实例设置标记。我在问题中添加了更多描述。如果我单击xib区域,ToucheSStart将不会进入If-else。
- (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
{
}
}