我为我的iOS应用创建了一个屏幕教程。
为了实现这一目标,我正在使用一个管理3个viewControllers的UIPageViewController。
- (void)setupContentViews{
UIViewController *screenTutorial1 = [[UIViewController alloc] initWithNibName:@"ScreenTutorial_1ViewController" bundle:nil];
UIViewController *screenTutorial2 = [[UIViewController alloc] initWithNibName:@"ScreenTutorial_2ViewController" bundle:nil];
tutorialPages = @[screenTutorial1, screenTutorial2];
}
一切都很好,除了当我更改screenTutorial1或screenTutorial2的背景时,它永远不会被调用。这是什么原因?有解决方案吗?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
NSLog(@"line 30)");
}
经过一些实验后,似乎如果我在UIPageViewController中添加代码(见下文),它会设置属性。但是如果我需要向View Controllers添加任何自定义方法呢?我是否需要从UIPageViewController做任何事情?
答案 0 :(得分:1)
问题是,此时view
为零,因此您无法更改backgroundColor
。
您应该将UIViewController
作为子类,并在backgroundColor
中设置viewDidLoad
。之后,您应该像{im>这样初始化tutorialPages
ivar的视图控制器:
YourUIViewControllerSubclass *screenTutorial1 = [[YourUIViewControllerSubclass alloc] initWithNimbName:@"ScreenTutorial_1ViewController" bunble:nil];
<强>更新强>
像这样更新您的方法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];
}