试图将苹果的页面控件示例合并到故事板视图控制器中

Posted

技术标签:

【中文标题】试图将苹果的页面控件示例合并到故事板视图控制器中【英文标题】:Attempting to incorporate apple's page control example into a storyboard viewcontroller 【发布时间】:2012-12-02 21:27:05 【问题描述】:

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

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

我想我已经足够了解这一点,它告诉我我没有选择器的方法......但我不知道如何解决它!提前谢谢你。

头文件 //TutorialViewController.h #导入

@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】:

你必须添加一个

- (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中有这样一个方法

【讨论】:

苹果演示中会不会是这种方法? // load the view nib and initialize the pageNumber ivar - (id)initWithPageNumber:(int)page if (self = [super initWithNibName:@"MyView" bundle:nil]) pageNumber = page; return self; AppleDemo 被命名为 PageControl 并且是编译干净的。当您查看该演示时,您会在 File PhoneContentViewController.m 中找到方法 loadScrollViewWithPage

以上是关于试图将苹果的页面控件示例合并到故事板视图控制器中的主要内容,如果未能解决你的问题,请参考以下文章

将故事板添加到现有项目

导航控制器无法在多个故事板中工作

在 Xcode 中合并故事板

将视图控制器从一个故事板移动或复制到另一个故事板

应用程序试图在目标上呈现一个 nil 模态视图控制器

故事板 - 弹出到视图控制器然后推送导致多次推送