Today i would ask you if there is a way to call a method of another view controller just not initializing it or just how to get that viewController as a global variable declared in .h file.
Help me please.
I’m using a PageViewController and I have to use a method of the contentViewController alredy initialized.
here I create my ViewControllers:
- (void)createTheContent {
NSLog(@"createTheContent");
pageController = nil;
//[pageController removeFromParentViewController];
//[pageController awakeFromNib];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: options];
pageController.dataSource = self;
[[pageController view] setFrame:CGRectMake(0, 0, 320, 365)];
initialViewController = [self viewControllerAtIndex:0];
NSMutableArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:pageController];
[[self view] addSubview:[pageController view]];
[pageController didMoveToParentViewController:self];
}
here are my propertys in .h file:
contentViewController *initialViewController;
}
//Page View Controller Properties
@property (strong, nonatomic) UIPageViewController *pageController;
@property (strong, nonatomic) NSArray *pageContent;
@property (strong, nonatomic) NSMutableArray *pageStrings;
@property (strong, nonatomic) id dataObject;
From your question title, it seems you are trying to access your controller to get some data back.
In this case, my suggestion would be creating a “model” (as in Model-View-Controller) in your design, and make that model accessible from everywhere in your app. An easy way to accomplish the latter point is to make the model class a singleton; or you might make it a class with only class-methods to give access to the data. See also this other answer of mine about the same issue for some sample code.
After reading your edit, I would suggest the following:
add to your
contentViewControllerclass a property say itdataSource;dataSourceis of typecontentViewControllerDataSourceand provides the method to access all required data, e.g.:before adding
contentViewControllerto the page controller, set itsdataSourceproperty:let your
selfcontroller implement thecontentViewControllerDataSourceprotocol:from
contentViewControlleryou will be able to call:or:
Hope this helps.
EDIT:
After looking at your files:
Please, remove the
#import "ViewController.h"directive at the beginning ofcontentViewController.h. That should fix the problem you had.Another thing: I wrote at point 1 that
contentViewControllerhave the propertydataSource;ViewControllerimplements thecontentViewControllerDataSourceprotocol and hosts the data thatcontentViewControllerwould like to access. One class is the “client” of the data; the other “shares” the data through the delegate protocol that you can modify from outside. In your code both thedataSourceand the protocol are assigned to the sameViewControllerclass and this makes no sense. So, you can try an fix it: I am not sure now which class should be doing what, but you will know, I hope.