user3071579
user3071579

Reputation: 85

How do I change the background color of a view controller managed by a UIPageViewController?

I created an onscreen tutorial for my iOS app.

To accomplish this I'm using a UIPageViewController which is managing 3 viewControllers.

- (void)setupContentViews{
UIViewController *screenTutorial1 = [[UIViewController alloc] initWithNibName:@"ScreenTutorial_1ViewController" bundle:nil];
UIViewController *screenTutorial2 = [[UIViewController alloc] initWithNibName:@"ScreenTutorial_2ViewController" bundle:nil];
tutorialPages = @[screenTutorial1, screenTutorial2];

}

Everything works great, except that when I got to change the background for screenTutorial1 or screenTutorial2 it never gets called. What's the reason for this? Is there a solution?

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
NSLog(@"line 30)");
}

After some experimentation it appears that if I add the code in UIPageViewController (see below) it sets the property. But what if I need to add any custom methods to my View Controllers? Do I need to do everything from UIPageViewController?

enter image description here

Upvotes: 0

Views: 66

Answers (1)

arturgrigor
arturgrigor

Reputation: 9441

The problem is that at that point the view is nil so you can't change the backgroundColor.

You should subclass UIViewController and set the backgroundColor in viewDidLoad. And after that you should initialize the view controllers for the tutorialPages ivar like this:

YourUIViewControllerSubclass *screenTutorial1 = [[YourUIViewControllerSubclass alloc] initWithNimbName:@"ScreenTutorial_1ViewController" bunble:nil];

Update

Update your method setupContentViews like this:

- (void)setupContentViews
{
    ScreenTutorial_1ViewController *screenTutorial1 = [[ScreenTutorial_1ViewController alloc] initWithNibName:@"ScreenTutorial_1ViewController" bundle:nil];
    UIViewController *screenTutorial2 = [[UIViewController alloc] initWithNibName:@"ScreenTutorial_2ViewController" bundle:nil];
    tutorialPages = @[screenTutorial1, screenTutorial2];
}

Upvotes: 1

Related Questions