我有一個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