简体   繁体   中英

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?

在此处输入图片说明

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM