简体   繁体   English

如何使用SubView的SuperView初始化UIView

[英]How to initialize a UIView using SuperView of a SubView

I have a UILabel that is the subview of a UIView, I would like to know how to use this UILabel to cast a new temporary UIView so that I can access its values and or its superview. 我有一个UILabel是UIView的子视图,我想知道如何使用这个UILabel来构建一个新的临时UIView,以便我可以访问它的值和/或它的superview。

to explain my object I have a UIScrollView which contains a UIView which in turn contains a UILabel, I would like to know how I can access the UIScrollView attributes when all I have access to is the UILabel. 解释我的对象我有一个UIScrollView,其中包含一个UIView,而UIView又包含一个UILabel,我想知道当我可以访问的是UILabel时,我如何访问UIScrollView属性。

This is how I created the object 这就是我创建对象的方式

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

Then I have a method that I am using to detect the new touch, add a value into the UILabel then check to see if the next UILabel is in frame or not. 然后我有一个方法,我用来检测新的触摸,在UILabel中添加一个值,然后检查下一个UILabel是否在帧中。 if it isn't then I am going to hopefully write some code to move the UIScrollViews scroll position. 如果不是那么我希望编写一些代码来移动UIScrollViews滚动位置。

this is that method 这就是那个方法

- (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];

}

I have also tried print a output of the view hierarchy to see if using .superview.superview is correct and this is a copy and paste of the selected object/view 我还尝试打印视图层次结构的输出,以查看是否使用.superview.superview是正确的,这是所选对象/视图的复制和粘贴

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

which I am now wondering if the cast of the label in my symbolButtonPressed method dose not allow access to superviews? 我现在想知道我的symbolButtonPressed方法中的标签的演员是否不允许访问超级视图?

The simple way would be: 简单的方法是:

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

This code assumes a fixed hierarchy of your views. 此代码假定您的视图的固定层次结构。 There are safer ways to walk up a view's superviews until you find the proper parent view. 有更安全的方法可以查看视图的超级视图,直到找到正确的父视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM