1

我在选项卡中有一个 UIScrollview,我在 UIScrollview 中添加了一些 UIView (NIB)。每个 UIView 都有一些 UISwictch、标签、按钮等。我怎样才能将视图内的 label.text 添加到 UIScrollview。

我尝试了很多东西,但我可以访问添加到 UIScrollview 的 UIView 的内容。

4

3 回答 3

9

检查这个,

for (UIView *addedView in [self.scrollView subviews])
{
        for (UIView *sub in [addedView subviews])
        {
            if([sub isKindOfClass:[UILabel class]])
            {
                UILabel *myLabel = (UILabel *)sub;
                NSLog(@"My label :%@",myLabel .text);
            }
        }
}

scrollView 是您的滚动视图。它将打印所有标签的文本。

如果您需要打印任何特定标签的文本。然后为其添加标签并在打印之前检查标签,例如if(myLabel.tag == 7)

于 2012-07-18T18:55:31.770 回答
5

tag为您的标签设置一个唯一的。

eg:(label.tag = 10345一些随机数,这是一个唯一的标签)

parentView你可以像这样搜索标签

UILabel *theLabel = (UILabel *)[parentView viewWithTag: 10345];

然后你可以用标签做任何你想做的事情。

于 2012-07-18T18:59:43.223 回答
0

第 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];
    }
}
于 2013-11-30T12:56:33.123 回答