[英]How to initialize a UIView using SuperView of a SubView
我有一個UILabel是UIView的子視圖,我想知道如何使用這個UILabel來構建一個新的臨時UIView,以便我可以訪問它的值和/或它的superview。
解釋我的對象我有一個UIScrollView,其中包含一個UIView,而UIView又包含一個UILabel,我想知道當我可以訪問的是UILabel時,我如何訪問UIScrollView屬性。
這就是我創建對象的方式
UIScrollView *containerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 77.0)];
insertView = [[UIView alloc] init];
insertView.frame = CGRectMake(0.0, 0.0, scrollWidth+10, 77.0);
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 5.0, 40.0, 40.0];
[insertView addSubView:myLabel];
[containerScrollView addSubView:insertView];
然后我有一個方法,我用來檢測新的觸摸,在UILabel中添加一個值,然后檢查下一個UILabel是否在幀中。 如果不是那么我希望編寫一些代碼來移動UIScrollViews滾動位置。
這就是那個方法
- (void) SymbolButtonPressed:(NSString *)selectedString {
UILabel *label = (UILabel *)[self.view viewWithTag:currentlySelectedTag];
if ([selectedString isEqualToString:@"Blank"]) {
[label setText:@" "];
[cutFieldsMutableArray replaceObjectAtIndex:currentlySelectedTag-1 withObject:@" "]; //replace first string with second string in the array
} else {
NSRange slashRange = [selectedString rangeOfString:@"/"];
if (slashRange.location != NSNotFound) {
NSString *updatedString = [[selectedString substringToIndex:slashRange.location] stringByAppendingString:@"+"];
[label setText:updatedString];
[cutFieldsMutableArray replaceObjectAtIndex:currentlySelectedTag-1 withObject:updatedString]; //replace first string with second string in the array
} else {
[label setText:selectedString];
[cutFieldsMutableArray replaceObjectAtIndex:currentlySelectedTag-1 withObject:selectedString]; //replace first string with second string in the arrayg
}
}
currentlySelectedTag ++;
if (totalCutCount < currentlySelectedTag) {
currentlySelectedTag = 1;
}
// reset tags so that you can set the correctly
totalCount = 0; // zero this out so that the next time the view is loaded you can get an accurate count of cuts
labelTag = 1;
labelPositionTag = 1001;
[self.tableView reloadData];
// test code to see if I can access the UIScrollView to move its scroll position using the Label object I cast at the start of this method
// this code is not working.
UIScrollView *temptemp = ((UIScrollView *)label.superview.superview); // this equals nil
// Sort out new position
float newPosition = ((UIScrollView *)label.superview.superview).contentOffset.x+label.superview.superview.frame.size.width;
CGRect toVisible = CGRectMake(newPosition, 0, label.superview.superview.frame.size.width, label.superview.superview.frame.size.height);
// perform scroll to new position
[(UIScrollView *)label.superview.superview scrollRectToVisible:toVisible animated:YES];
}
我還嘗試打印視圖層次結構的輸出,以查看是否使用.superview.superview是正確的,這是所選對象/視圖的復制和粘貼
*** UPDATE
<UIScrollView: 0x1676d560; frame = (0 0; 320 77); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x16636b20>; layer = <CALayer: 0x16659970>; contentOffset: {0, 0}>
| | | | | | | | <UIView: 0x16637280; frame = (0 0; 404 18); layer = <CALayer: 0x16643980>>
| | | | | | | | <UIView: 0x16655da0; frame = (0 0; 414 77); layer = <CALayer: 0x16643950>>
| | | | | | | | | <UILabel: 0x166ce3b0; frame = (2 35; 40 40); text = ' '; clipsToBounds = YES; tag = 1; gestureRecognizers = <NSArray: 0x166cf650>; layer = <CALayer: 0x16645dc0>>
我現在想知道我的symbolButtonPressed方法中的標簽的演員是否不允許訪問超級視圖?
簡單的方法是:
UILabel *label = ... // the reference to your label
UIScrollView *scrollView = (UIScrollView *)label.superview.superview.superview;
此代碼假定您的視圖的固定層次結構。 有更安全的方法可以查看視圖的超級視圖,直到找到正確的父視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.