gpt4 book ai didi

iPhone Dev- Page Control - scrollViewDidScroll 没有触发

转载 作者:行者123 更新时间:2023-12-01 17:33:55 25 4
gpt4 key购买 nike

我是 iPhone 开发新手,正在学习本教程:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 - 但我在 XCode 4.3.2 中这样做。该示例非常简单,我理解代码,但由于某种原因,我的 ViewController.m 中的 scrollViewDidScroll 函数没有为我触发(因此这不会更改页面控件分页图标并将其更新到当前页面)。我在这个函数中放置了一个 NSLog() 以便我可以判断它是否正在激活 - 但在我的调试控制台中我从来没有看到这个文本

我的 ViewController.h 中的代码是:

 #import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView* scrollView;
UIPageControl* pageControl;

}

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

@end

我的 ViewController.m 中的代码是:
 #import "ViewController.h"
@implementation ViewController
@synthesize scrollView, pageControl;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;

UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}


//!!!!!!!!!!!!!!
//THIS IS WHERE MY PROBLEM IS -- THIS CODE DOESN'T SEEM TO EVER GET RUN EVEN WHEN I SCROLL!!!!!
//!!!!!!!!!!!!!
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(@"this just fired");

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


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
self.scrollView = nil;
self.pageControl = nil;
}


- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}

@end

任何帮助将不胜感激。我一遍又一遍地从他们在教程中的可下载文件到我自己的文件来回移动......我只是觉得当我开始滚动时这个东西应该触发并且不知道为什么它不是......提前致谢!

最佳答案

在 Interface Builder 中,您没有将 ScrollView 的“委托(delegate)”导出连接到您的 View Controller 。但是您可以通过编程方式进行操作。

[self.scrollView setDelegate:self];

关于iPhone Dev- Page Control - scrollViewDidScroll 没有触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10958661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com