以编程方式实现UIScrollView

在Apple的页面控件示例中,界面构建器中有一个ScrollView。 它与相应的IBOutlet相关联。 我想更改代码,所以这一切都是以编程方式完成的。 我删除了界面构建器对象,删除了IBOutlet关键字。 我分配并初始化scrollView,但是当我运行程序时没有出现。

我假设这是因为我需要将它作为子视图分配给主视图。 或者我呢? 我仍然不太了解所有观点是如何工作和相互作用的。 如果我这样做[self.view addSubView:ScrollView]; 我得到一个运行时错误(或者其他东西,它通常只是说像BAD ACCESS或SIGABRT)。

我究竟做错了什么? 我完全走错了路吗? (只有两天的ios编程,在树林里仍然有点迷失)

手机内容控制器中的awakeFromNib:

 - (void)awakeFromNib { scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // load our data from a plist file inside our app bundle NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"]; self.contentList = [NSArray arrayWithContentsOfFile:path]; // view controllers are created lazily // in the meantime, load the array with placeholders which will be replaced on demand NSMutableArray *controllers = [[NSMutableArray alloc] init]; for (unsigned i = 0; i < kNumberOfPages; i++) { [controllers addObject:[NSNull null]]; } self.viewControllers = controllers; [controllers release]; // a page is the width of the scroll view scrollView.pagingEnabled = YES; scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.scrollsToTop = NO; scrollView.delegate = self; pageControl.numberOfPages = kNumberOfPages; pageControl.currentPage = 0; // pages are created on demand // load the visible page // load the page on either side to avoid flashes when the user starts scrolling // [self loadScrollViewWithPage:0]; [self loadScrollViewWithPage:1]; } 

头文件:

 #import  #import  #import "ContentController.h" @interface PhoneContentController : ContentController  { UIScrollView *scrollView; UIPageControl *pageControl; NSMutableArray *viewControllers; // To be used when scrolls originate from the UIPageControl BOOL pageControlUsed; } @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) IBOutlet UIPageControl *pageControl; @property (nonatomic, retain) NSMutableArray *viewControllers; - (IBAction)changePage:(id)sender; @end 

的appDelegate:

 #import "AppDelegate.h" #import "ContentController.h" @implementation AppDelegate @synthesize window, contentController; - (void)dealloc { [window release]; [contentController release]; [super dealloc]; } - (void)applicationDidFinishLaunching:(UIApplication *)application { NSString *nibTitle = @"PadContent"; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { nibTitle = @"PhoneContent"; } [[NSBundle mainBundle] loadNibNamed:nibTitle owner:self options:nil]; [self.window addSubview:self.contentController.view]; [window makeKeyAndVisible]; } @end 

并且已从xib文件中删除scrollView。 注意:这是下载程序的新版本,其中我所有更改的是删除scrollView的IBOutlet关键字,从xib中删除滚动并从nib中清醒地添加alloc,init行。

我已经建议更改appDelegate并将awakeFromNib更改为init方法,我已经尝试了所有这些,但它仍然无法正常工作。

由于您没有从nib文件加载接口,因此您应该在PhoneContentControllerinit方法中设置UIScrollView

 - (id)init { [super init]; if (self) { scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)]; // Place it where you want it. viewControllers = [[NSMutableArray alloc] init]; // load our data from a plist file inside our app bundle NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"]; self.contentList = [NSArray arrayWithContentsOfFile:path]; // view controllers are created lazily // in the meantime, load the array with placeholders which will be replaced on demand NSMutableArray *controllers = [[NSMutableArray alloc] init]; for (unsigned i = 0; i < kNumberOfPages; i++) { [controllers addObject:[NSNull null]]; } self.viewControllers = controllers; [controllers release]; // a page is the width of the scroll view scrollView.pagingEnabled = YES; scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.scrollsToTop = NO; scrollView.delegate = self; pageControl.numberOfPages = kNumberOfPages; pageControl.currentPage = 0; // pages are created on demand // load the visible page // load the page on either side to avoid flashes when the user starts scrolling // [self loadScrollViewWithPage:0]; [self loadScrollViewWithPage:1]; } return self; } 

AppDelegate ,进行以下更改:

 - (void)applicationDidFinishLaunching:(UIApplication *)application { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { contentController = [[PhoneContentController alloc] init]; } else { contentController = [[PadContentController alloc] init]; } [self.window addSubview:contentController.view]; [window makeKeyAndVisible]; } 

你只需将它放在awakeFromNib方法中

 scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)]; 

不需要添加为子视图,因为在phoneContentController xib里面没有视图。所以如何添加像[self.view addSubView:ScrollView];这是因为phoneContentController不是UIViewController类的类型。 它是ContentController的子类,它是该项目中NSObject的子类。

简单方法:如果需要手段,可以多次创建

 - (void)viewDidLoad { [super viewDidLoad]; int x = 0; int y = 10; for(int i=0; i<5; i++) { UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)]; scrollview.showsVerticalScrollIndicator=YES; scrollview.scrollEnabled=YES; scrollview.userInteractionEnabled=YES; scrollview.backgroundColor = [UIColor greenColor]; [self.view addSubview:scrollview]; //scrollview.contentSize = CGSizeMake(50,50); x=x+55; //[self myscrollView]; } }