如何使用子视图的Supperiew初始化UIVIEW



我有一个uilabel,它是uiview的子视图,我想知道如何使用此uilabel施放新的临时Uiview。

要解释我的对象,我有一个uiscrollview,其中包含一个uiview,又包含一个uilabel,我想知道当我可以访问的一切都是uilabel。

这就是我创建对象的方式

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是正确的,这是所选对象/view 的副本和粘贴

***更新

<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>>

我现在想知道标签在我的符号buttonpressed方法剂量中是否不允许访问Supperiews?

简单的方法是:

UILabel *label = ... // the reference to your label
UIScrollView *scrollView = (UIScrollView *)label.superview.superview.superview;

此代码假定您的视图的固定层次结构。有更安全的方法可以在查看正确的父景观之前走上视图的监督。

最新更新