【问题标题】:How can i get the content of a subview in a UiScrollview如何在 UiScrollview 中获取子视图的内容
【发布时间】:2012-07-18 18:49:48
【问题描述】:

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

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

【问题讨论】:

  • 只需引用 UIScrollView (@property, iVar, global var) 和 -addSubview。不需要for循环的废话。
  • 我完全同意@CodaFi,不需要那种for循环的东西,特别是因为OP不知道如何引用UIView(学习如何做到这一点比学习更重要简单地拥有有效的代码)...

标签: ios uiview uiscrollview


【解决方案1】:

检查一下,

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)

【讨论】:

    【解决方案2】:

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

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

    你可以像这样在parentView中搜索标签

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

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

    【讨论】:

      【解决方案3】:

      第 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];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2017-05-25
        • 2018-01-21
        • 2015-06-17
        相关资源
        最近更新 更多