0

请帮助我并告诉我我的代码何时出现错误。我想为此制作带有分页模式的滚动视图,我为 UIScroll 和 UIPageControl 制作了两个 View One,另外一个 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 文件我使用情节提要。

4

0 回答 0