[英]How do I change the background color of a view controller managed by a UIPageViewController?
I created an onscreen tutorial for my iOS app. 我为我的iOS应用创建了一个屏幕教程。
To accomplish this I'm using a UIPageViewController which is managing 3 viewControllers. 为此,我使用一个UIPageViewController,它管理3个viewController。
- (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. 一切都很好,除了当我必须更改screenTutorial1或screenTutorial2的背景时,它从未被调用。 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. 经过一番试验后,看来如果我在UIPageViewController中添加代码(请参见下文),则会设置该属性。 But what if I need to add any custom methods to my View Controllers?
但是,如果我需要向视图控制器中添加任何自定义方法怎么办? Do I need to do everything from UIPageViewController?
我需要从UIPageViewController做所有事情吗?
The problem is that at that point the view
is nil so you can't change the backgroundColor
. 问题在于,此时的
view
为nil,因此您无法更改backgroundColor
。
You should subclass UIViewController
and set the backgroundColor
in viewDidLoad
. 您应该子类化
UIViewController
并在viewDidLoad
设置backgroundColor
。 And after that you should initialize the view controllers for the tutorialPages
ivar like this: 之后,您应该像这样初始化
tutorialPages
ivar的视图控制器:
YourUIViewControllerSubclass *screenTutorial1 = [[YourUIViewControllerSubclass alloc] initWithNimbName:@"ScreenTutorial_1ViewController" bunble:nil];
Update 更新
Update your method setupContentViews
like this: 像这样更新您的方法
setupContentViews
:
- (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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.