程序在scrollViewDidScroll处收到信号exc_bad_access

发布于 2024-12-09 11:22:48 字数 2628 浏览 3 评论 0原文

在我的应用程序中,我尝试使用 pageController 执行滚动视图。 pageController 正在工作,但滚动视图不工作。当我尝试转到下一页时,我总是在 [self loadScrollViewWithPage:page];

从方法 scrollViewDidScroll

处收到 program returned signal exc_bad_access我可以不知道我的赌注在哪里。请看一下这段代码...

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getVehicules];

    vosvehiculeScrollView.pagingEnabled = YES;
     vosvehiculeScrollView.contentSize = CGSizeMake(vosvehiculeScrollView.frame.size.width * pageControlVehiculePossedee.numberOfPages, vosvehiculeScrollView.frame.size.height);
    vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
    vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
    vosvehiculeScrollView.scrollsToTop = NO;

    pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
    pageControlVehiculePossedee.currentPage=0;
    [pageControlVehiculePossedee addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    for (int i=0; i<pageControlVehiculePossedee.numberOfPages;i++){
        [self loadScrollViewWithPage:i];
    }

}

- (void) loadScrollViewWithPage: (int) page {
    if (page < 0) return;
    if (page >= [vehiculesPossede count]) return;    

    CGRect frame = vosvehiculeScrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    viewVehicules.frame = frame;
    [vosvehiculeScrollView addSubview:viewVehicules];

    NSLog(@"%d",page);

}

-  (void) scrollViewDidScroll: (UIScrollView *) sender {
    if (pageControlUsed) {
        return;
    }

    CGFloat pageWidth = vosvehiculeScrollView.frame.size.width;
    int page = floor((vosvehiculeScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControlVehiculePossedee.currentPage = page;

    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

}

- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
    pageControlUsed = NO;
}

- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
    pageControlUsed = NO;
}

-(void)pageAction:(UIPageControl*)control
{       
    NSLog(@"page changed"); 

    int page = pageControlVehiculePossedee.currentPage;
    NSLog(@"page %d", page);

    CGRect frame;
    frame.origin.x = self.vosvehiculeScrollView.frame.size.width * self.pageControlVehiculePossedee.currentPage;
    frame.origin.y = 0;
    frame.size = self.vosvehiculeScrollView.frame.size;
    [self.vosvehiculeScrollView scrollRectToVisible:frame animated:YES];

    pageControlUsed = YES;
}

In my app I tried to do a scrollView with a pageController. The pageController is working but the scrollView is not working. When I try to go to next page I always get program received signal exc_bad_access at line [self loadScrollViewWithPage:page];

from method scrollViewDidScroll

I can't see where is my mystake. Please look at this code...

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getVehicules];

    vosvehiculeScrollView.pagingEnabled = YES;
     vosvehiculeScrollView.contentSize = CGSizeMake(vosvehiculeScrollView.frame.size.width * pageControlVehiculePossedee.numberOfPages, vosvehiculeScrollView.frame.size.height);
    vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
    vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
    vosvehiculeScrollView.scrollsToTop = NO;

    pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
    pageControlVehiculePossedee.currentPage=0;
    [pageControlVehiculePossedee addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    for (int i=0; i<pageControlVehiculePossedee.numberOfPages;i++){
        [self loadScrollViewWithPage:i];
    }

}

- (void) loadScrollViewWithPage: (int) page {
    if (page < 0) return;
    if (page >= [vehiculesPossede count]) return;    

    CGRect frame = vosvehiculeScrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    viewVehicules.frame = frame;
    [vosvehiculeScrollView addSubview:viewVehicules];

    NSLog(@"%d",page);

}

-  (void) scrollViewDidScroll: (UIScrollView *) sender {
    if (pageControlUsed) {
        return;
    }

    CGFloat pageWidth = vosvehiculeScrollView.frame.size.width;
    int page = floor((vosvehiculeScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControlVehiculePossedee.currentPage = page;

    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

}

- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
    pageControlUsed = NO;
}

- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
    pageControlUsed = NO;
}

-(void)pageAction:(UIPageControl*)control
{       
    NSLog(@"page changed"); 

    int page = pageControlVehiculePossedee.currentPage;
    NSLog(@"page %d", page);

    CGRect frame;
    frame.origin.x = self.vosvehiculeScrollView.frame.size.width * self.pageControlVehiculePossedee.currentPage;
    frame.origin.y = 0;
    frame.size = self.vosvehiculeScrollView.frame.size;
    [self.vosvehiculeScrollView scrollRectToVisible:frame animated:YES];

    pageControlUsed = YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文