Rob*_*bie 1 iphone objective-c ios xcode4.3
我是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
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.我一直在他们在教程上的可下载文件来回反复...我只是觉得当我开始滚动并且不知道它为什么不是......提前致谢!
在Interface Builder中,您没有将滚动视图的"委托"插座连接到视图控制器.但是你可以通过编程方式完成它.
[self.scrollView setDelegate:self];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4717 次 |
| 最近记录: |