试图将apple的页面控制示例合并到storyboard viewcontroller中

时间:2012-12-02 21:27:05

标签: ios uiscrollview uipagecontrol pagecontrol

我有一些图像,我试图在滚动视图中加载,类似于苹果的页面控制示例。我的应用程序因

错误而崩溃
  

'NSInvalidArgumentException',原因:' - [TutorialViewController   loadScrollViewWithPage:]:无法识别的选择器发送到实例   0x687d0b0'

我想我已经足够了解它告诉我,我没有选择器的方法......但我不确定如何解决它!提前谢谢。

头文件     //TutorialViewController.h     #import

@interface TutorialViewController : UIViewController <UIScrollViewDelegate>
{    
    // To be used when scrolls originate from the UIPageControl
    BOOL pageControlUsed;

    int pageNumber;
}


@property (nonatomic, retain) NSArray *iPhoneTutorial;

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;

@property (nonatomic, retain) NSMutableArray *viewControllers;

- (IBAction)changePage:(id)sender;

@end

实施档案

//TutorialViewController.m
#import "TutorialViewController.h"
static NSUInteger kNumberOfPages = 3;

static NSString *NameKey = @"nameKey";
static NSString *ImageKey = @"imageKey";

@interface TutorialViewController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end

@implementation TutorialViewController
@synthesize scrollView, pageControl;

- (void)awakeFromNib
{
    // load our data from a plist file inside our app bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:@"iPhoneTutorial" ofType:@"plist"];
    self.iPhoneTutorial = [NSArray arrayWithContentsOfFile:path];

    // view controllers are created lazily
    // in the meantime, load the array with placeholders which will be replaced on demand
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++)
    {
        [controllers addObject:[NSNull null]];
    }
    //self.viewControllers = controllers;
    //[controllers release];

    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

    pageControl.numberOfPages = kNumberOfPages;
    pageControl.currentPage = 0;

    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    //
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
}


// load the view nib and initialize the pageNumber ivar
- (id)initWithPageNumber:(int)page
{
    pageNumber = page;
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    if (pageControlUsed)
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    // A possible optimization would be to unload the views+controllers which are no longer visible
}

// 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;
}

- (IBAction)changePage:(id)sender
{
    int page = pageControl.currentPage;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];

    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
}


- (void)viewDidLoad

{
    // Do any additional setup after loading the view, typically from a nib.
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:1)

您必须添加

- (void)loadScrollViewWithPage:(int)page

添加视图的方法

喜欢

switch(page) {
   case 0:
   myView1 = [[MyView1ViewController alloc] initWithNibName:@"MyView1ViewController" bundle:nil];
   [scrollView addSubview: myViewPage1.view]; 
  break;
}

但Apple Demo“页面控制”在File Phone ContentViewController.m中有这样一种方法