Ios 使用UIPagecontrol的基于Tabbarcontroller的应用程序-崩溃
我有一个基于标签栏的应用程序,效果很好。但是我想使用UIPagecontrol允许用户在视图之间滑动 我一直在使用教程为我的应用程序实现这一点。但本教程和我的应用程序之间的区别在于,我的应用程序基于选项卡栏系统 UIscrollview基于pageViewController类的第一个选项卡选项,连接子视图位于IntroViewController中 故事板布局如下所示 生成错误的原因是Ios 使用UIPagecontrol的基于Tabbarcontroller的应用程序-崩溃,ios,objective-c,uitabbarcontroller,uistoryboard,uipageviewcontroller,Ios,Objective C,Uitabbarcontroller,Uistoryboard,Uipageviewcontroller,我有一个基于标签栏的应用程序,效果很好。但是我想使用UIPagecontrol允许用户在视图之间滑动 我一直在使用教程为我的应用程序实现这一点。但本教程和我的应用程序之间的区别在于,我的应用程序基于选项卡栏系统 UIscrollview基于pageViewController类的第一个选项卡选项,连接子视图位于IntroViewController中 故事板布局如下所示 生成错误的原因是 Terminating app due to uncaught exception 'NSRangeExc
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
两个类的标题
#import <UIKit/UIKit.h>
@interface simpleMain : UIViewController <UIScrollViewDelegate>
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
@end
#import "simpleMain.h"
@interface simpleMain ()
@property (assign) BOOL pageControlUsed;
@property (assign) NSUInteger page;
@property (assign) BOOL rotating;
- (void)loadScrollViewWithPage:(int)page;
@end
@implementation simpleMain
@synthesize scrollView;
@synthesize pageControl;
@synthesize pageControlUsed = _pageControlUsed;
@synthesize page = _page;
@synthesize rotating = _rotating;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setPagingEnabled:YES];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
[self loadScrollViewWithPage:i];
}
self.pageControl.currentPage = 0;
_page = 0;
[self.pageControl setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= [self.childViewControllers count])
return;
// replace the placeholder if necessary
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller == nil) {
return;
}
// add the controller's view to the scroll view
if (controller.view.superview == nil) {
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollView addSubview:controller.view];
}
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
@end
#import "simpleMain.h"
@interface miniViewController : simpleMain {
}
@property (strong, nonatomic) IBOutlet UIView *View1;
@property (strong, nonatomic) IBOutlet UIView *View2;
@property (strong, nonatomic) IBOutlet UIView *View3;
@end
#import "MiniViewController.h"
@interface miniViewController ()
@end
@implementation miniViewController
@synthesize View1;
@synthesize View2;
@synthesize View3;
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View3"]];
}
@end
#导入
@接口simpleMain:UIViewController
@属性(非原子,强)IBUIScrollView*scrollView;
@属性(非原子,强)IBUIPageControl*pageControl;
-(iAction)更改页:(id)发件人;
@结束
#导入“simpleMain.h”
@接口simpleMain()
@使用的属性(分配)BOOL pagecontrol;
@属性(分配)页面;
@属性(赋值)布尔旋转;
-(void)加载滚动视图带页面:(int)页面;
@结束
@实现simpleMain
@综合滚动视图;
@综合页面控制;
@综合pageControlUsed=\u pageControlUsed;
@综合页面=_页面;
@综合旋转=_旋转;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[self.scrollView setPaginEnabled:是];
[self.scrollView设置可滚动:是];
[self.scrollView设置ShowShorizontalScrollIndicator:否];
[self.scrollView设置ShowsVerticalScrollIndicator:否];
[self.scrollView setDelegate:self];
}
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
对于(i=0;i<[self.childViewControllers count];i++){
[带页面的自动加载滚动视图:i];
}
self.pageControl.currentPage=0;
_page=0;
[self.pageControl setNumberOfPages:[self.ChildViewController计数];
UIViewController*viewController=[self.childViewController对象索引:self.pageControl.currentPage];
if(viewController.view.superview!=nil){
[视图控制器视图将显示:动画];
}
self.scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*[self.childViewControllers count],scrollView.frame.size.height);
}
-(void)加载滚动视图带页面:(int)页面{
如果(第<0页)
返回;
如果(第>=[self.childview控制器计数])
返回;
//如有必要,请替换占位符
UIViewController*控制器=[self.childViewController对象索引:页];
如果(控制器==nil){
返回;
}
//将控制器视图添加到滚动视图
if(controller.view.superview==nil){
CGRect frame=self.scrollView.frame;
frame.origin.x=frame.size.width*页;
frame.origin.y=0;
controller.view.frame=frame;
[self.scrollView addSubview:controller.view];
}
}
//在滚动拖动开始时,重置滚动源自UIPageControl时使用的布尔值
-(无效)scrollView将开始刷新:(UIScrollView*)scrollView{
_pageControlUsed=否;
}
//在滚动动画结束时,重置滚动源自UIPageControl时使用的布尔值
-(无效)ScrollViewDiEndDecelling:(UIScrollView*)scrollView{
_pageControlUsed=否;
}
@结束
#导入“simpleMain.h”
@界面迷你视图控制器:simpleMain{
}
@属性(强,非原子)ibuiview*View1;
@属性(强,非原子)ibuiview*View2;
@属性(强,非原子)ibuiview*View3;
@结束
#导入“MiniViewController.h”
@接口miniViewController()
@结束
@迷你视图控制器的实现
@综合视图1;
@综合视图2;
@综合视图3;
-(无效)viewDidLoad
{
//加载视图后,通常从nib执行任何其他设置。
[超级视图下载];
[self addChildViewController:[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“View1”];
[self addChildViewController:[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“View2”];
[self addChildViewController:[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“View3”];
}
@结束
(我知道错误是这样读的,我试图在空数组中找到一个元素。我只是不知道数组来自何处以及它与此设置的连接)
任何帮助都会很好。从我在故事板和代码中看到的情况来看,调用
视图时,pageViewController没有任何子视图控制器。这就是导致崩溃的原因。放弃了本教程,因为我发现它有很多缺陷。问题已解决。查看您的代码将非常有帮助。同时添加一个异常断点以查看正在崩溃的行。当然,将立即更新问题。错误消息表明数组不包含值。NSLog您的数组要查看它的上下文,请记住数组索引从0开始问题的底部是这样的:(我知道错误是这样读的,我正在尝试在空数组中查找一个元素。我只是不知道数组来自何处以及它与此安装程序的连接)。基本上,我知道没有NSArray。[self.childViewControllers objectAtIndex:]在调用objectAtIndex之前,请确保此数组(self.childViewControllers)具有足够的元素(计数>索引):在教程页面上,它使用该代码编译。我很困惑。。。。。。(我认为代码已经准备就绪,可以用于任何我想做类似事情的项目)。@element是您的IntroViewController
继承表单pageViewController吗?如果不这样做,它将继承,我认为您的代码将工作,在其接口中使用@interface pageViewController:UIViewController。没有运气。对各种导航控制器有点困惑。在IntroViewController.h
文件中,检查是否选中了@interface IntroViewController:pageViewController
,它将