我是 iOS 的新程序员。当我尝试使用分页对 ScrollView 进行编码时,有一个问题需要解决。我真的不知道我的代码有什么问题。我已经设置了 pageEnable,并将页码设置为 4 当前 0。在 xib.file 中,我创建了 4 个视图,使滚动视图将“委托”连接到“文件所有者”。我的参考链接http://www.iosdevnotes.com/2011/03/uiscrollview-paging/ 当我运行应用程序时,视图无法分页。谢谢您的帮助。这是我的代码:
///////// 我认为viewDidLoad中的for循环有问题。
进口
@interface ThirdViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;
}
- (IBAction)pageValueChange:(id)sender;
@property(nonatomic,strong) IBOutlet UIScrollView *scrollView;
@property(nonatomic,strong) IBOutlet UIView *contentOne;
@property(nonatomic,strong) IBOutlet UIView *contentTwo;
@property(nonatomic,strong) IBOutlet UIView *contentThree;
@property(nonatomic,strong) IBOutlet UIView *contentFour;
@property(nonatomic,strong) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong) NSArray *viewArray;
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()<UIScrollViewDelegate>
@end
@implementation ThirdViewController
@synthesize scrollView,contentOne, contentTwo,contentThree,contentFour,pageControl,viewArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Profile", @"Third");// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
viewArray = [[NSArray alloc]initWithObjects:@"contentOne",@"contentTwo",@"contentThree", @"contentFour",nil];
for (int i =0; i < [viewArray 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];
[self.scrollView addSubview: subview]; //////how to pass the array object to subview
[subview removeFromSuperview];
}
// Do any additional setup after loading the view from its nib.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *[viewArray count], scrollView.frame.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)dismissKeyboard :(id) sender{
[sender resignFirstResponder];
}
- (IBAction)pageValueChange:(id)sender{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
}
#pragma mark -UIScrollView Delegate
-(void)scrollViewDidScroll:(UIScrollView *)sender{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
self.pageControl.currentPage = page;
}
@end