Попытка включить пример управления страницей Apple в контроллер просмотра раскадровки

У меня есть несколько изображений, которые я пытаюсь загрузить в режиме прокрутки, подобно примеру управления страницей Apple. Мое приложение вылетает с ошибкой

«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

person Macness    schedule 02.12.2012    source источник


Ответы (1)


Вы должны добавить

- (void)loadScrollViewWithPage:(int)page

метод, в котором вы добавляете свое представление

подобно

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

Но в Apple Demo «PageControl» есть такой метод в файле PhoneContentViewController.m.

person AlexWien    schedule 02.12.2012
comment
Может ли быть этот метод в демонстрации яблок? // load the view nib and initialize the pageNumber ivar - (id)initWithPageNumber:(int)page { if (self = [super initWithNibName:@"MyView" bundle:nil]) { pageNumber = page; } return self; } - person Macness; 03.12.2012
comment
AppleDemo называется PageControl и компилируется чисто. Когда вы посмотрите на эту демонстрацию, вы найдете в файле PhoneContentViewController.m метод loadScrollViewWithPage - person AlexWien; 03.12.2012