我有一个名为TabView的自定义UIView类。每个TabView中都有2个标签以及其他一些元素。我使用界面构建器将这些标签添加到TabView。在我的视图控制器中,将TabView作为子视图,我使用以下方法将触摸事件附加到每个TabView
- (void)viewDidLoad
{
[super viewDidLoad];
for (UIView *tabView in self.view.subviews) {
if([tabView isKindOfClass:[DeviceTabView class]]){
[self addGestureRecogniser:tabView];
}
}
}
-(void)addGestureRecogniser:(UIView *)touchView{
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(segueToDeviceUpload:)];
[touchView addGestureRecognizer:singleTap];
}
我需要能够从单击的UIView中获取两个标签值,以便我可以将其传递给以下视图控制器。我知道如何在ViewControllers之间传递值,只是不确定如何从选定的TabView中获取标签。谢谢你的指导!
答案 0 :(得分:2)
您可以通过浏览视图的子视图来获取文本,类似于您上面已经执行的操作:
- (void)segueToDeviceUpload:(UIGestureRecognizer *)gestureRecognizer {
for (UIView *view in gestureRecognizer.view.subviews) {
if([view isKindOfClass:[UILabel class]]){
NSLog(@"One piece of text: %@",((UILabel *)view).text);
}
}
}
您可以使用标签来区分每个标签。
答案 1 :(得分:1)
您可以从传递给segueToDeviceUpload的UIGestureRecognizer
获取所选的tabView:via gestureRecognizer.view。我建议为每个标签分配一些tag
,以便您可以通过viewWithTag从tabView获取它们:
一下子:
-(void)segueToDeviceUpload:(UITapGestureRecognizer*)sender{
UIView *tappedView = sender.view;
UILabel *label1 = (UILabel*)[tappedView viewWithTag:1];
UILabel *label2 = (UILabel*)[tappedView viewWithTag:2];
}