Aluminum
Aluminum

Reputation: 2992

Access UIScrollView content offset in a container view from the parent view controller

I have a problem accessing the ContentOffset of a UIScrollView. I had no problem with this before, but this time the UIScrollView is inside a Container View linked to a ViewController.

To better understand my situation here's a scheme and a picture:

MenuViewController => Container View => ScrollView1ViewController => ScrollView1

enter image description here

The UIScrollView which I have to get it's ContentOffset is linked using an IBOutlet in the ScrollView1ViewController.h.

So my question is: is it possible to know the ContentOffset of the UIScrollView from the MenuViewController.m? Maybe importing the IBOutlet inside ScrollView1ViewController.h in MenuViewController.m

ScrollView1ViewController.h

#import <UIKit/UIKit.h>

@interface ScrollView1ViewController : UIViewController <UIScrollViewDelegate>
{
}

@property (weak, nonatomic)    IBOutlet    UIScrollView    *scrollView1;

@end

MenuViewController.m

#import "MenuViewController.h"
#import "ScrollView1ViewController.h"

@interface MenuViewController ()
{
}

@property (strong, nonatomic)    ScrollView1ViewController    *sV1VC;
@property (weak, nonatomic)      UIScrollView                 *scrollView1;

@end

/***/

- (void)scrollForward
{
    self.offsetPoint = self.scrollView1.contentOffset.x;

    if ((self.offsetPoint == 0))
    {
        [self.scrollView1 setContentOffset:CGPointMake(270, 0) animated:YES];
    }
}

If you need more code and/or pictures to better understand my problem don't hesitate to ask me.

Thanks

Upvotes: 2

Views: 570

Answers (2)

Geet
Geet

Reputation: 2437

The one way in which I could see this is if you make a function to return the contentOffset of your scrollview in your ScrollView1ViewController

- (CGRect)returnContentOffSet
{
    return myScrollView.contentOffset;
}

and the try to call that method from MenuViewController

Upvotes: 1

Zhang
Zhang

Reputation: 11607

When you setup your container view controller, you must have stored a reference to your ScrollView1ViewController as a property or an instance inside an array in the event your have multiple sub view controllers:

@interface MenuViewController

@property (nonatomic, strong) ScrollView1ViewController *subVC1;
...

@end

So given, that you have a reference to the ScrollView1ViewController, can you simply not go:

// ------------------------------------------------
// Example button tap to get scrollview offset
// this method is inside your MenuViewController.m
// ------------------------------------------------
-(void)buttonTapped:(id)sender
{
    CGPoint contentOffset = self.subVC1.scrollView.contentOffset;

    NSLog(@"ScrollView contentOffset = %lf, %lf", contentOffset.x, contentOffset.y);
}

Note:

I'm assuming here, in your ScrollView1ViewController, you have a reference to the scrollView as a property.

Does that help?

Upvotes: 0

Related Questions