以编程方式实现UIScrollView



在苹果的页面控件示例中,界面生成器中有一个ScrollView。它与相应的IBOutlet链接。我想更改代码,这样这一切都是用程序完成的。我删除了接口构建器对象,我删除了IBOutlet关键字。我分配并初始化了scrollView,但在运行程序时什么都没有出现。

我认为这是因为我需要将它作为子视图分配给主视图。还是我?我仍然不太明白所有的观点是如何运作和相互作用的。如果我执行[self.view addSubView:ScrollView];,我会得到一个运行时错误(或者其他什么,它通常只是说BAD ACCESS或SIGABRT之类的东西)。

我做错了什么?我是不是完全走错了路?(只进行了两天的ios编程,仍然有点迷失在树林中)

唤醒手机内容控制器中的Nib:

- (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 <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ContentController.h"
@interface PhoneContentController : ContentController <UIScrollViewDelegate>
{   
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

并且scrollView已从xib文件中删除。注意:这是下载程序的新版本,我所做的只是删除了scrollView的IBOutlet关键字,删除了xib中的滚动,并在nib中添加了唤醒中的alloc,init行。

我有人建议我更改appDelegate,并将wakeFromNib更改为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];
}

您只需将其放入wakeFromNib方法中

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];
    }
}

最新更新