如何使用Page Control和UIScrollView自定义每个页面的内容?

发布于 2024-08-02 04:57:52 字数 1310 浏览 11 评论 0原文

我在使用 pagecontrol 和 UIScrollView 自定义每个页面时遇到问题。 我正在自定义 Apple 的 页面控制

基本上我想让每个页面都不同,文本和图像交替出现在不同的页面上。第 1 页将包含所有文本,第 2 页将仅包含图像,第 3 页将包含所有文本并继续。

这是原始代码:

    // Set the label and background color when the view has finished loading.
    - (void)viewDidLoad {
        pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
        self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
}

如您所见,当您向右滚动时,此代码仅显示第 1 页、第 2 页等。

我尝试输入这个新代码,但这没有任何区别。没有错误。 我知道这是非常简单的代码。我不明白为什么它不起作用。

我将 pageText 声明为 UILabel。

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber == 1) {
        pageText.text = @"Text in page 1";
    }
    if (pageNumber == 2) {
            pageText.text = @"Image in page 2";
    }
    if (pageNumber == 3) {
            pageText.text = @"Text in page 3";
    }

}

我不知道为什么它不起作用。另外,如果您有更好的方法,请告诉我。谢谢。

I have problem with customizing each page using pagecontrol and UIScrollView.
I'm customizing Page Control from Apple.

Basically I would like to have each page different with text and image alternately on different page. Page 1 will have all text, Page 2 will have just images, Page 3 will have all text and goes on.

This is original code:

    // Set the label and background color when the view has finished loading.
    - (void)viewDidLoad {
        pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
        self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
}

As you can see, this code shows only Page 1, Page 2 etc as you scroll right.

I tried to put in this new code but that didn't make any difference. There's no error.
I know this is pretty simple code. I don't why it doesn't work.

I declare pageText as UILabel.

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber == 1) {
        pageText.text = @"Text in page 1";
    }
    if (pageNumber == 2) {
            pageText.text = @"Image in page 2";
    }
    if (pageNumber == 3) {
            pageText.text = @"Text in page 3";
    }

}

I don't know why it doesn't work. Also if you have better way to do it, let me know. Thanks.

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

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

发布评论

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

评论(3

陈年往事 2024-08-09 04:57:52

我下载了示例,将以下行添加到 MyViewController.h

IBOutlet UILabel *pageText;

@property (nonatomic, retain) UILabel *pageText;

在 MyViewController.m 中添加了 @synthesize pageText;行和您的 viewDidLoad 内容。

我在界面生成器中打开 MyView.xib/en 文件并连接新标签。编译并运行,大部分都有效。唯一看起来不对劲的是它落后了一页。所以我重写了它以适当地处理奇数页或偶数页。

- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber % 2) {
        pageText.text = [NSString stringWithFormat:@"Image in page %d", pageNumber + 1];
    } else {
        pageText.text = [NSString stringWithFormat:@"Text in page %d", pageNumber +1];
    }   
}

I downloaded the sample, added the following lines to the MyViewController.h

IBOutlet UILabel *pageText;

@property (nonatomic, retain) UILabel *pageText;

In the MyViewController.m I added the @synthesize pageText; line and your viewDidLoad contents.

I opened the MyView.xib/en file in interface builder and connected the new label. Compiled and ran and it worked for the most part. Only thing that didn't seem right was that it was a page behind. so I rewrote it to just handle odd or even pages appropriately.

- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber % 2) {
        pageText.text = [NSString stringWithFormat:@"Image in page %d", pageNumber + 1];
    } else {
        pageText.text = [NSString stringWithFormat:@"Text in page %d", pageNumber +1];
    }   
}
人间☆小暴躁 2024-08-09 04:57:52

这个答案的代码很少,因为我没有明显的例子。您基本上希望拥有一些页面数组,其中包含每个页面的视图(或视图控制器)。数组中的每个视图/控制器不必相同(尽管您应该将其设置为所有视图或所有视图控制器,而不是两者)。完成后,您可以根据要加载的页面加载不同的内容。

那有意义吗?

This answer will have very little code, as I have no distinct example. You basically want to have some array of pages, which holds a view (or view controller) for each page. There is no necessity that each view/controller in the array be the same (though you should have it be all views or all view controllers, not both). Once you have that, you can load something distinct depending on which page you're loading.

Does that make sense?

好多鱼好多余 2024-08-09 04:57:52

尝试从 SVN 存储库下载该项目。我无法完成文档,但查看了 IGUIScrollViewElements ...有一个 IGUIScrollViewImage 的简短示例,代码非常相似...我会尽力尽快完成其余示例...

< a href="http://code.google.com/p/iphone-guru-uilibrary/" rel="nofollow noreferrer">http://code.google.com/p/iphone-guru-uilibrary/

try to download this project from SVN repo. I've been unable to finish the documentation but check out IGUIScrollViewElements ... there is a short example for IGUIScrollViewImage and the code is quite similar ... I'll do my best to finish examples for the remaining ones asap ...

http://code.google.com/p/iphone-guru-uilibrary/

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