基于Tabbarcontroller的应用程序使用UIPagecontrol - 崩溃

时间:2013-07-13 14:50:41

标签: ios objective-c uitabbarcontroller uistoryboard uipageviewcontroller

我有基于标签栏的应用程序,效果很好。但是我想使用UIPagecontrol来允许用户在视图之间滑动。

我一直在使用教程http://www.samsurge.com/p/blog-page.html来实现我的应用。但是教程和我的应用程序之间的区别在于我的应用程序基于标签栏系统。

UIscrollview基于第一个选项卡选项和类pageViewController,连接子视图位于IntroViewController中。

故事板布局如下所示。

http://threepointdesign.co.uk/img2.png

生成错误

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

(我知道错误读取,我试图在里面找到一个元素和空数组。我只是不知道数组来自何处以及它与此设置有什么联系。)

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

从我在故事板和代码中看到的内容,当调用childViewControllers时,pageViewController没有任何viewWillAppear。所以这导致了崩溃。

答案 1 :(得分:0)

放弃本教程,因为我发现它有很多缺陷。问题解决了。