1

我有一些图像我试图在类似于苹果页面控件示例的滚动视图中加载。我的应用程序因错误而崩溃

'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
4

1 回答 1

1

你必须添加一个

- (void)loadScrollViewWithPage:(int)page

添加视图的方法

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

但是Apple Demo“PageControl”在File PhoneContentViewController.m中有这样一个方法

于 2012-12-02T21:35:45.913 回答