簡體   English   中英

我如何在UiScrollview中獲取子視圖的內容

[英]How can i get the content of a subview in a UiScrollview

我在選項卡中有一個UIScrollview,我向UIScrollview添加了一些UIView(NIB)。 每個UIView都有一些UISwictch,標簽,按鈕等。如何在添加到UIScrollview的視圖內部獲取label.text。

我嘗試了很多事情,但是可以訪問添加到UIScrollview的UIView的內容。

檢查一下

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)

設置一個唯一的tag為標簽。

例如: label.tag = 10345 (一些隨機數,這是唯一的標簽)

您可以像這樣在parentView搜索標簽

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

然后您就可以使用Label做任何您想做的事情。

步驟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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM