StuartM
StuartM

Reputation: 6823

Getting access to property on Page View Controller

I have a workflow setup as follows:

Navigation Controller
-VC1
-VC2
-VC3
-PageVC
--VC4
--VC5

I pass an object for example a UIImage reference through the first controllers up to the PageVC. Once here the PageVC loads the controllers as follows:

- (void) viewDidLoad {
    [super viewDidLoad];

    self.dataSource = self;

    _side = [self.storyboard instantiateViewControllerWithIdentifier: @"GuessGameTurnWordVC"];
    _center = [self.storyboard instantiateViewControllerWithIdentifier: @"GuessGameTurnMainVC"];

    [self setViewControllers: @[_center]
                   direction: UIPageViewControllerNavigationDirectionForward
                    animated: NO
                  completion: nil];
}

- (UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if ( viewController == _center )
    {
        _side.title = @"right";
        return _side;
    }

    if ( viewController == _side && [_side.title isEqualToString: @"left"] )
    {
        return _center;
    }

    return nil;
}

- (UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if ( viewController == _center )
    {
        _side.title = @"left";
        return _side;
    }

    if ( viewController == _side && [_side.title isEqualToString: @"right"] )
    {
        return _center;
    }

    return nil;
}

Question Within the VC4/VC5 I need to access a property from the PageVC. How can I do this?
Do I need to pass the property down to these controllers too, or can I access it directly from its parent in someway?

Upvotes: 0

Views: 1638

Answers (2)

artud2000
artud2000

Reputation: 544

You can access directly from it's parents. for parent's you meant they are subclasses of PageVC? Then just access it using VC4.propertyName if by parent's you meant viewController hierarchy within the NavigationController then you have different options.

The easiest one might be to create a property within your VC4 and VC5 then when you instantiate them just pass the value you want to use.

_side = [self.storyboard instantiateViewControllerWithIdentifier: @"GuessGameTurnWordVC"];
_side.someValue = something;

If you are going to use a variable or value over all your VC then I would suggest a singleton. Hope this helps

Upvotes: 0

Lukas
Lukas

Reputation: 41

Use the Singleton design pattern, write in your PageVC.m:

static NSObject yourObject;

@implementation PageVC

    +(NSObject*)getThisObject{
       if(!yourObject){
          yourObject = [[NSObject alloc] init];
          // init your object how you need it
       }
       return yourObject;
    }

@end

and write in the PageVC.h:

+(NSObject*) getThisObject;

Now you can get this property in any class with this line:

NSObject *obj = [PageVC getThisObject];

But remember to include the PageVC.h in the other VCs.

I hope this was useful

Upvotes: 1

Related Questions