基于 xcode 页面的应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10060281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode page-based application
提问by user1319851
I am trying to create an app based on the page-based application template in xcode 4, that has a title page, table of contents page, and content pages, all formatted differently. I am new to ios programming, completed the 3 apple tutorials and read a lot of documentation but this is basically my first non-tutorial app.
我正在尝试基于 xcode 4 中基于页面的应用程序模板创建一个应用程序,该应用程序具有标题页、目录页和内容页,所有格式都不同。我是 ios 编程的新手,完成了 3 个苹果教程并阅读了大量文档,但这基本上是我的第一个非教程应用程序。
I'd really appreciate a code example or instructions that shows how to do this properly. This particular template does not seem to have many examples out there.
我真的很感激一个代码示例或说明,展示了如何正确地做到这一点。这个特定的模板似乎没有很多例子。
specifically:
具体来说:
- I created a new xcode project and chose the page-based application template (i'm not including the source code here because it should be easy to generate for anyone with xcode 4)
- The template creates an AppDelegate, a RootViewController, a DataViewController and a ModelController
- For now, I don't care about showing dynamic data. I want the first page to show a view with a label that says "Title" and the
second page and after to show a text area filled with lorem ipsum.
I have created a TitlePageView and ChapterView in the storyboard and corresponding ViewController files, but do not know how to tie them to the DataViewController so that they display.
- 我创建了一个新的 xcode 项目并选择了基于页面的应用程序模板(我在这里不包括源代码,因为对于任何使用 xcode 4 的人来说,它应该很容易生成)
- 该模板创建了一个 AppDelegate、一个 RootViewController、一个 DataViewController 和一个 ModelController
- 目前,我不关心显示动态数据。我希望第一页显示一个带有“标题”标签的视图,第二页和之后显示一个填充了 lorem ipsum 的文本区域。
我已经在storyboard和对应的ViewController文件中创建了TitlePageView和ChapterView,但是不知道如何将它们绑定到DataViewController以便显示。
回答by AMayes
Yeah, I'm not sure how to do it using storyboard, but if you want a simple cut-and-paste solution using nibs, I posted one here: https://stackoverflow.com/a/9687639/542400
是的,我不确定如何使用故事板来做到这一点,但如果你想要一个使用笔尖的简单剪切和粘贴解决方案,我在这里发布了一个:https: //stackoverflow.com/a/9687639/542400
回答by user1319851
This is how I ended up putting different views on each page.
这就是我最终在每个页面上放置不同视图的方式。
- Create the views for each page in the storyboard, and leave them without a segue or connection to the main page. (you get yellow warning for this)
- Set the 'identifier' property for each new view to some unique string, e.g. "titlePage"
- Create an outlet for the big white part of the default page view called "contentArea" in the DataViewController
Update the DataViewController class viewWillAppear method to create your page views and add them as sub-views to the contentArea:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_contentArea.subviews.count == 0) { UIView *pageSubView; UIViewController *pageSubViewController; pageSubViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"titlePage"]; [self addChildViewController: pageSubViewController]; [pageSubViewController didMoveToParentViewController:self]; pageSubView = pageSubViewController.view; [_contentArea addSubview:pageSubView]; CGRect pageViewRect = _contentArea.bounds; pageSubView.frame = pageViewRect; } }
- 为故事板中的每个页面创建视图,并让它们与主页面没有转场或连接。(你会收到黄色警告)
- 将每个新视图的“标识符”属性设置为某个唯一的字符串,例如“titlePage”
- 在 DataViewController 中为名为“contentArea”的默认页面视图的大白色部分创建一个出口
更新 DataViewController 类的 viewWillAppear 方法以创建页面视图并将它们作为子视图添加到 contentArea:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_contentArea.subviews.count == 0) { UIView *pageSubView; UIViewController *pageSubViewController; pageSubViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"titlePage"]; [self addChildViewController: pageSubViewController]; [pageSubViewController didMoveToParentViewController:self]; pageSubView = pageSubViewController.view; [_contentArea addSubview:pageSubView]; CGRect pageViewRect = _contentArea.bounds; pageSubView.frame = pageViewRect; } }
回答by libsemik
here is the code snippet for creating a page
这是用于创建页面的代码片段
pageInfoLabel.text = [NSString stringWithFormat:@"Some really useless information on page %d", pageNumber + 1];