以编程方式实现 UIScrollView

发布于 2024-12-28 00:04:48 字数 3292 浏览 4 评论 0原文

在苹果的页面控制示例中,有一个界面生成器中的 ScrollView。它与相应的 IBOutlet 链接。我想更改代码,以便这一切都以编程方式完成。我删除了界面生成器对象,删除了 IBOutlet 关键字。我分配并初始化滚动视图,但是当我运行程序时什么也没有出现。

我认为这是因为我需要将其作为子视图分配给主视图。或者我也这样?我仍然不太明白所有视图如何工作以及如何相互作用。如果我这样做 [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 <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文件中删除。注意:这是下载程序的新版本,其中我所做的所有更改是删除滚动视图的 IBOutlet 关键字,从 xib 中删除滚动并添加 alloc,从 nib 唤醒的 init 行。

我有人建议更改 appDelegate 并将 awakeFromNib 更改为 init 方法,我已经尝试了所有这些,但它仍然不起作用。

In the page control sample from apple there is a ScrollView in the interface builder. It is linked with the corresponding IBOutlet. I want to change the code so this is all done programatically. I delete the interface builder object, I delete the IBOutlet keyword. I alloc and init the scrollView, but nothing appears when I run the program.

I assume this is because I need to assign it as a subView to the main view. Or do I? I still don't really understand how all the views work and interact with each other. If I do [self.view addSubView:ScrollView]; I get a runtime error (or something, it usually just says something like BAD ACCESS or SIGABRT).

What am I doing wrong? Am I on the wrong path completely? (only two days in to ios programming, still a bit lost in the woods)

awakeFromNib in phone content controller:

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

header file:

#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

and the scrollView has been deleted from the xib file. Note: this is a new version of the downloaded program where all I have changed is deleting the IBOutlet keyword for the scrollView, deleted the scroll from the xib and added the alloc, init line in awake from nib.

I've had suggestions to change the appDelegate and change the awakeFromNib to an init method, i've tried all this but it still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罗罗贝儿 2025-01-04 00:04:48

由于您不是从 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];
}

Since you're not loading the interface from a nib file, you should set up your UIScrollView in your PhoneContentController's init method:

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

In your AppDelegate, make the following changes:

- (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];
}
甜柠檬 2025-01-04 00:04:48

您只需将其放入 awakeFromNib 方法中,

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

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

you simply put this inside awakeFromNib method

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

no need to add as subview because in phoneContentController xib there are no views inside that.so how can you add like[ self.view addSubView:ScrollView];this is because phoneContentController is not type of UIViewController Class. it's a sub Class of ContentController which is subClass of NSObject inside that project.

神魇的王 2025-01-04 00:04:48

方法简单:如果需要的话可以多次创建

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

Simple Method: You can created multiple times if you need means

- (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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文