如何使用SubView的SuperView初始化UIView

时间:2014-01-20 21:11:44

标签: ios objective-c uiview uiscrollview uilabel

我有一个UILabel是UIView的子视图,我想知道如何使用这个UILabel来构建一个新的临时UIView,以便我可以访问它的值和/或它的超级视图。

解释我的对象我有一个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方法中的标签的演员是否不允许访问超级视图?

1 个答案:

答案 0 :(得分:2)

简单的方法是:

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

此代码假定您的视图具有固定的层次结构。在找到合适的父视图之前,有更安全的方法来查看视图的超视图。