如何实际实现Apple示例中的分页UIScrollView?

发布于 2024-09-19 01:54:39 字数 1601 浏览 7 评论 0原文

我在 Apple 的开发人员文档中找到了一个分页 UIScrollView 的示例,这正是我的应用程序想要的。我有一个带有“帮助”按钮的主视图,我想呈现一个用户可以翻阅以查看所有帮助主题的视图。示例位于:

https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html%23//apple_ref/doc/uid/TP40008179 -CH3-SW1

它的工作原理与广告中所宣传的一样,但是,与许多 Apple 的示例一样,它们直接在 AppDelegate 中执行各种代码。我有自己的 AppDelegate,但是我有一个 NavigationController 和一个 RootView,上面有这个“帮助”按钮。我似乎无法将示例代码集成到我自己的代码中并使其工作。我很确定我不能将他们在 AppDelegate 中的代码放入我自己的代码中,但如何设置它却让我困惑。

有人可以给我举一个他们做我所说的事情的例子吗?

编辑:我能够创建一个新项目,并通过将所有 AppDelegate 方法移动到模板提供的 UIViewController 中并创建一个新的 来让它像 Apple 一样工作>ContentPage UIViewController 来保存内容。它可以从一个页面滚动到另一个页面,所以我想我可以将此代码插入到我的其他项目中。

我用等效的 viewDidLoad 替换了 applicationDidFinishLaunching ,并摆脱了处理 window 等的 AppDelegate 内容。然后我更改了 Apple 的 initWithPageNumber: 方法来引用我的帮助页面,而不仅仅是创建其通用视图的实例。

- (id)initWithPageNumber:(int)page {
    NSLog(@"--initWithPageNumber:ContentPage");
    if (self = [super initWithNibName:[NSString stringWithFormat:@"HelpPage%d", page] bundle:nil]) {
        pageNumber = page;
    }
    return self;
}

感谢您的帮助 - 有时有人告诉您可以继续前进,这很好!

I found an example of a paging UIScrollView in Apple's developer docs and it's just what I want for my application. I have a main view with a "Help" button that I want to present a view that users can page through to see all the help topics. The sample is at:

https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html%23//apple_ref/doc/uid/TP40008179-CH3-SW1

And it works as advertised, but, as with so many of Apple's examples, they do all kinds of code right in the AppDelegate. I have my own AppDelegate, but then I have a NavigationController with a RootView that has this "Help" button on it. I can't seem to get the example code integrated into my own and have it work. I'm pretty sure I can't put the code they have in their AppDelegate in my own, but how to set it up eludes me.

Can someone point me to an example where they do what I'm talking about?

EDIT: I was able to create a new project and get it to work like Apple's by moving all the AppDelegate methods into the UIViewController that the template supplied and creating a new ContentPage UIViewController to hold the content. It can scroll from page to page, so I think I can insert this code into my other project ok.

I replaced the applicationDidFinishLaunching with an equivalent viewDidLoad and got rid of the AppDelegate stuff dealing with window and such. Then I changed Apple's initWithPageNumber: method to refer to my help pages rather than just creating instances of their generic views.

- (id)initWithPageNumber:(int)page {
    NSLog(@"--initWithPageNumber:ContentPage");
    if (self = [super initWithNibName:[NSString stringWithFormat:@"HelpPage%d", page] bundle:nil]) {
        pageNumber = page;
    }
    return self;
}

Thanks for the help - sometimes it's good just to have someone tell you it can be done to keep going!

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

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

发布评论

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

评论(1

秋风の叶未落 2024-09-26 01:54:39

在示例代码中,applicationDidFinishLaunching: 方法设置 UIScrollView(和 UIPageControl,但我们暂时忽略它),然后“加载“前两页。 “加载”页面包括将该页面的视图控制器加载到内存中并将其缓存。然后,它将该控制器的视图作为子视图添加到 UIScrollView 偏移量中,并根据其“页码”使用适当的 x。您应该能够在 viewDidLoad 中完成所有这些操作。

如果显示视图控制器的视图时显示空白视图,则说明您尚未将子视图正确添加到 UIScrollView 中。根据您具体更改的内容,这可能是 (a) 未正确获取 x 偏移、(b) 未正确设置框架或 (c) 未正确将其添加为子视图的某种组合。

如果您发布一些自己的代码,我们也许能够阐明该问题。

In the sample code, the applicationDidFinishLaunching: method sets up the UIScrollView (and a UIPageControl, but let's ignore that for now) and then "loads" the first two pages. "Loading" a page consists of loading that page's view controller into memory and caching it. It then adds that controller's view as a subview to the UIScrollView offset with an appropriate x based on what "page number" it is. You should be able to do all that in your viewDidLoad.

If you have a blank view when your view controller's view is shown, then you haven't added the subview correctly to your UIScrollView. Depending on what exactly you changed, that could be some combination of (a) not getting the x offset correct, (b) not setting the frame correctly, or (c) not adding it as a subview correctly.

If you post some of your own code we might be able to shed some light on the problem.

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