如何实际实现Apple示例中的分页UIScrollView?
我在 Apple 的开发人员文档中找到了一个分页 UIScrollView
的示例,这正是我的应用程序想要的。我有一个带有“帮助”按钮的主视图,我想呈现一个用户可以翻阅以查看所有帮助主题的视图。示例位于:
它的工作原理与广告中所宣传的一样,但是,与许多 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在示例代码中,
applicationDidFinishLaunching:
方法设置UIScrollView
(和UIPageControl
,但我们暂时忽略它),然后“加载“前两页。 “加载”页面包括将该页面的视图控制器加载到内存中并将其缓存。然后,它将该控制器的视图作为子视图添加到UIScrollView
偏移量中,并根据其“页码”使用适当的 x。您应该能够在viewDidLoad
中完成所有这些操作。如果显示视图控制器的视图时显示空白视图,则说明您尚未将子视图正确添加到
UIScrollView
中。根据您具体更改的内容,这可能是 (a) 未正确获取 x 偏移、(b) 未正确设置框架或 (c) 未正确将其添加为子视图的某种组合。如果您发布一些自己的代码,我们也许能够阐明该问题。
In the sample code, the
applicationDidFinishLaunching:
method sets up theUIScrollView
(and aUIPageControl
, 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 theUIScrollView
offset with an appropriate x based on what "page number" it is. You should be able to do all that in yourviewDidLoad
.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.