我正在尝试使用包含一些UIViewControllers的pageControl的UIScrollView创建一个“指令”窗口:
- (void)loadScrollViewWithPage:(NSInteger)page {
if (page < 0 || page >= kNumberOfPages) return;
switch (page) {
case 0:
[[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];
break;
case 1:
[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
break;
case 2:
[[FirstTabViewController alloc] initWithNibName:@"FirstTabViewController" bundle:nil];
break;
case 3:
[[SecondTabViewController alloc] initWithNibName:@"SecondTabViewController" bundle:nil];
break;
case 4:
[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
break;
default:
[[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];
break;
}
}
我无法让它发挥作用。
滚动有效,但我无法显示我制作的任何UIViewController!
我怎样才能让它发挥作用?
答案 0 :(得分:0)
尝试使用UIPageViewController并添加许多不同的视图控制器(及其滚动视图)。
P.S。 UIPageViewController是iOS 5及更高版本。
答案 1 :(得分:0)
听起来你对快速解决方案更感兴趣而不是正确行事。好的。这是你将如何做到的。
- (void)loadScrollViewWithPage:(NSInteger)page {
if (page < 0 || page >= kNumberOfPages) return;
UIViewController * newController = NULL;
switch (page) {
case 0:
newController = (UIViewController *)[[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];
break;
case 1:
newController = (UIViewController *)[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
break;
case 2:
newController = (UIViewController *)[[FirstTabViewController alloc] initWithNibName:@"FirstTabViewController" bundle:nil];
break;
case 3:
newController = (UIViewController *)[[SecondTabViewController alloc] initWithNibName:@"SecondTabViewController" bundle:nil];
break;
case 4:
newController = (UIViewController *)[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
break;
default:
newController = (UIViewController *)[[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];
break;
}
if(newController)
{
// this method is inside a UIScrollView, right?
[self.contentView addSubview: newController.view];
}
// at this point, newController has been alloc'd and retained. You should either release it next time you enter
// into loadScrollViewWithPage (i.e. declare newController as an object in your .h file) or you should
// add "autorelease" after all of those initWithNibName calls up there
}