【问题标题】:Tabbarcontroller based app using UIPagecontrol - Crashes使用 UIPagecontrol 的基于 Tabbarcontroller 的应用程序 - 崩溃
【发布时间】:2013-07-13 14:50:41
【问题描述】:

我有一个基于标签栏的应用程序,效果很好。但我想使用 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

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

任何帮助都会很棒。

【问题讨论】:

  • 查看您的代码真的很有帮助。还要添加一个异常断点来查看崩溃的行。
  • 好的,马上更新问题。
  • 您的错误消息表明该数组不包含值。 NSLog 你的数组以查看它的上下文,记住数组索引从 0 开始
  • 问题的底部写着:(我知道错误读取,我试图在里面找到一个元素和空数组。我只是不知道数组来自哪里以及它有什么连接到这个设置)。基本上没有我知道的 NSArray。
  • [self.childViewControllers objectAtIndex:] 确保这个数组(self.childViewControllers)有足够的元素(count>index),然后再调用objectAtIndex:

标签: ios objective-c uitabbarcontroller uistoryboard uipageviewcontroller


【解决方案1】:

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

【讨论】:

  • samsurge.com/p/blog-page.html 在教程页面上,它使用该代码编译。我很困惑......(我认为代码是开箱即用的,可以用于我想做类似事情的任何项目)。
  • @Evilelement 是你的IntroViewController 继承表单 pageViewController 吗?如果不让它继承,我认为你的代码会起作用,
  • 在其界面中使用@interface pageViewController : UIViewController 。没有运气。对各种导航控制器感到有些困惑。
  • 在您的IntroViewController.h 文件中检查您是否有@interface IntroViewController : pageViewController
  • 检查,它是接口 IntroViewController : UIViewController 但尝试了接口 IntroViewController : UIPageViewController
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多