我有一个UIScrollview在选项卡中,我添加了一些UIView(NIB)到UIScrollview。每个UIView都有一些UISwictch,标签,按钮等。我如何获得视图里面的label.text添加到UIScrollview中。我怎样才能得到UiScrollview的子视图的内容
我尝试了很多东西,但我可以访问添加到UIScrollview的UIView的内容。
我有一个UIScrollview在选项卡中,我添加了一些UIView(NIB)到UIScrollview。每个UIView都有一些UISwictch,标签,按钮等。我如何获得视图里面的label.text添加到UIScrollview中。我怎样才能得到UiScrollview的子视图的内容
我尝试了很多东西,但我可以访问添加到UIScrollview的UIView的内容。
检查与此,
这里scrollView
是滚动视图。它将打印所有标签的文本。
如果您需要打印任何特定标签的文本。然后添加标签并在打印之前检查该标签,如if(myLabel.tag == 7)
谢谢:D的作品! – 2012-07-18 19:07:29
感谢您的评论:) – 2012-07-18 19:08:34
为您的标签设置了独特的tag
。
如:label.tag = 10345
(一些随机的号码,这是一个独特的标签)
,你可以在parentView
搜索标签这样
UILabel *theLabel = (UILabel *)[parentView viewWithTag: 10345];
,然后你可以做你想做什么都与标签。
第1步:在您的滚动视图添加该代码
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTapGestureRecognizer];
第2步:实现此方法
-(void)singleTap:(UITapGestureRecognizer *)gesture
{
// Convert gesture into view for getting subviews
CGPoint point = [gesture locationInView:mainScrollView];
UIView *tappedView = [mainScrollView hitTest:point withEvent:nil];
// Get the subviews of the view
NSArray *subviews = [view tappedView];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(@"%@", subview);
// List the subviews of subview
[self your_method:subview];
}
}
只是要到的UIScrollView(@property,伊娃,全局变量)和参考 - addSubview。不需要for循环的废话。 – CodaFi 2012-07-18 18:56:15
我完全同意@CodaFi,不需要那些for循环的东西,特别是因为OP不知道如何引用UIView(学习如何做到这一点比单纯地使用代码更重要) ... – Cashew 2012-09-26 14:12:41