Question

I am making an app where I have a container view that is half the screen of my view controller. From the container view controller's class I am trying to access and manipulate a view that sits out side of the container view. (picture below)

enter image description here

I am trying to access and add items to the scroll view from the container view class like so:

parent = (GFProfileViewController*)[self parentViewController];
UIScrollView  *scroll = (UIScrollView *)[parent.view viewWithTag:222];
parent.titleHolders.contentSize = CGSizeMake(320 * 4,60);
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setFrame:CGRectMake(0, 0, 100, 40)];
[testLabel setText:@"My Test label"];
[parent.titleHolders addSubview:testLabel];
scroll.backgroundColor = [UIColor blueColor];

how ever does not work. I tried even accessing the view from the parents "view with tag" method.

neither works.

I know the code is fine because when I move it to the parent vc all works as expected. I need to be able to manipulate the view from the container though. Can anyone help?

Was it helpful?

Solution

As nhgrif says, don't do that.

You should treat another view controller's views as private.

Again, as nhgrif says, create a public method in the parent view controller that takes the information needed and does the displaying itself.

If the view controllers are just being initialized then the parent view controller's view hierarchy may not exist yet. In that case you'd want to set properties to hold the value(s) you want to display, and then display them in your viewWillAppear method.

With storyboards and iOS >= 6, you can set up the child view controller using an embed segue, and then in your prepareForSegue method you can set the parent view controller up as the child view controller's delegate. That's a clean way to have the child communicate back to the parent. (I have a sample app on github that demonstrates this technique if you need a more detailed explanation.)

OTHER TIPS

As far as programming ethics, correctness, good practices and whatnot, I think others have that covered. What you want to do surely isn't the clean way to go, but I often find myself going for such shortcuts, but you really need to know what you're doing, and if it isn't coming back to bite you later. The good thing is that it takes one line of code to achieve and has very low overhead. The problem is that such approach is highly dependent on the view structure, so if you change it, it will no longer work.

With that being said. What you tried doesn't work because the 'parentViewController' property isn't set on any arbitrary view. It should only be defined on the main view of the view controller, the one you can access from 'viewController.view'.

I will assume from your comment that your view structure is something like:

UIView
    UIView
        UIScrollView
    Container

So basically starting from Container you need to go up one level and then down to UIScrollView like this:

UIScrollView = [((UIView*)[self.superview.subviews objectAtIndex:0]).subviews objectAtIndex:0];

I am unsure if you can search by tag from the upper view, since the one you're searching for is somewhere nested inside, farther than one level down. Assuming you can actually do that, this should also work and be more fail proof.

UIView *upperView = self;
while([upperView superview]){
    upperView = [upperView superview];
}
UIScrollView *scroll = [upperView viewWithTag:222];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top