![](/img/trans.png)
[英]How can i get the content of a subview in a UIScrollview in a specific order according to a tag or something similar?
[英]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.