0

我有基於標籤欄的應用程序,它工作得很好。但我想使用UIPagecontrol來允許用戶在視圖之間滑動。基於Tabbarcontroller的應用程序使用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 

(我知道錯誤讀取,我試圖尋找內部和空數組的元素。我只是不知道數組來自哪裏以及它與這個設置有什麼聯繫)。

任何幫助將是偉大的。

+0

這真的有助於看到你的代碼。還要添加一個異常中斷點來查看崩潰的行。 – danypata

+0

當然,現在會更新問題。 – Evilelement

+0

您的錯誤消息指出該數組不包含值。 NSLog你的數組看看它的上下文,記得數組索引從0開始0 –

回答

0

從我在故事板和代碼中看到的情況來看,當調用viewWillAppear時,pageViewController沒有任何childViewControllers。所以這導致了崩潰。

+0

http://www.samsurge.com/p/blog-page.html教程頁面,它用該代碼編譯。我很困惑......(我認爲代碼已經準備就緒,可以用於任何想要做這種事情的項目)。 – Evilelement

+0

@Evilelement是你的'IntroViewController'繼承形式pageViewController?如果不是這樣,它會繼承,我認爲你的代碼將工作, – danypata

+0

在其界面中使用@interface pageViewController:UIViewController 。沒有運氣。與各種導航控制器有點混淆。 – Evilelement

0

因爲我發現它存在很多缺陷,所以將本教程拋棄。問題解決了。