请帮我告诉我代码中的错误。 我想用分页模式制作滚动视图,为此我制作了两个View 一个用于UIScroll& UIPageControl和另一个UIControllerView将它添加到UIScroll上。 我的问题是,当运行应用程序时,我可以在页面之间滚动,但滚动是空白的,没有视图出现在其中
这是UIScroll的代码
#import "ViewController.h"
#import "ViewController1.h"
@interface ViewController ()
- (void)loadScrollViewWithPage:(int)page ;
@end
@implementation ViewController
- (void)viewDidLoad
{
NSMutableArray * Controllers=[[NSMutableArray alloc]init];
for(int i=0;i<4;i++)
{
[Controllers addObject: [NSNull null]];
}
self.viewControllers1=Controllers;
[Controllers release];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4,
scrollview. frame. size.height );
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self. scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
[super viewDidLoad];
}
- (void)loadScrollViewWithPage:(int)page {
if(page<0) return;
if(page>=4)return;
ViewController1 *controller=[self.viewControllers1 objectAtIndex:page];
if((NSNull *)controller == [NSNull null])
{
controller =[[ViewController1 alloc]initWithPage:page];
[self.viewControllers1 replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollView addSubview: controller.view];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
if(PageControlUsed)return;
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.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;
}
- (IBAction)changePage:(id)sender {
int page=self.pageControl.currentPage;
[self loadScrollViewWithPage:page-1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page+1];
CGRect frame=self.scrollView.frame;
frame.origin.x=frame.size.width*page;
frame.origin.y=0;
[self.scrollView scrollRectToVisible:frame animated:YES];
PageControlUsed=YES;
}
- (void)dealloc {
[self.viewControllers1 release];
[self.scrollView release];
[self.pageControl release];
[super dealloc];
}
@end
这是视图控制器的代码,我想添加它来滚动它
#import "ViewController1.h"
@interface ViewController1 ()
@end
@implementation ViewController1
@synthesize label;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
-(id)initWithPage:(int)page
{
if(self=[super init])
{pageNum=page;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
label.text=[NSString stringWithFormat:@"Page %d " ,pageNum+1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(int)pageNum{
return pageNum;
}
@end
注意:我正在使用iOS 6我不使用XIB文件我使用故事板。