Chapter 2
Boom! App
In this chapter yoursquo;re going to create an iOS app that does something. Not much—these are early days—but enough to call it useful. In the process, you will:
- Use Xcodersquo;s Interface Builder to design your app
- Add objects to your app
- Connect objects together
- Customize your objects to provide content
- Add resource files to your project
- Use storyboards to create segues
- Control the layout of visual elements using constraints
Amazingly, yoursquo;re going to create this app without writing a single line of computer code. This is not typical, but it will demonstrate the flexibility of Xcode.
The app yoursquo;re going to create presents some interesting facts about women surrealists of the twentieth century. Letrsquo;s get started.
Design
Before firing up Xcode and typing furiously, you need to have a plan. This is the design phase of app development. Over the lifetime of your app, you may revise your design several times as you improve it, but before you begin you need a basic idea of what your app will look like and how you want it to work.
Your design may be written out formally, sketched on a napkin, or just be in your head. It doesnrsquo;t matter, as long as you have one. You need to, at the very least, be able to answer some basic questions. What kinds of devices will your app run on (iPhone/iPod, iPad, or both)? Will your app run in portrait mode, sideways, or both? What will the user see? How will the user navigate? How will they interact with it?
17
A rough sketch of this app is shown in Figure 2-1. The app is very simple, so it doesnrsquo;t require much in the way of initial design. The surrealist app will have an opening screen containing portraits of famous women surrealists. Tapping one will transition to a second screen showing a representative painting and a scrollable text field with information about the artistrsquo;s life. Yoursquo;ve decided this is going to run only on an iPhone or iPod Touch, and only in Portrait orientation. This will simplify your design and development.
Figure 2-1. Sketch of Surrealist app
Creating the Project
The first step is to create your project. Click the New Project button on the startup window or choose the New ➤ New Projecthellip; command. Review the available templates, as shown in Figure 2-2.
Figure 2-2. iOS project templates
Your design gives you a basic idea of how your app will work, which should suggest which Xcode project template to start with. Your apprsquo;s design isnrsquo;t a perfect fit with any of these, so choose the Single View Application template—itrsquo;s the simplest template that already has a view. Click on the Next button.
The next step is to fill in the details about your project (see Figure 2-3). Name the project
Surrealists and fill in your organization name and identifier. Consistent with your design choices, change the Devices option from Universal to iPhone, as shown in Figure 2-3.
Figure 2-3. Setting the project details
Note Developing for the iPhone is the same as developing for the iPod Touch (unless your app uses features only available on the iPhone). From here on, Irsquo;ll only mention the iPhone, but please remember that this also includes the iPod Touch.
Therersquo;s also a Class Prefix setting. This option sets a very short string that will be used to consistently name all new classes you add to your project. Itrsquo;s traditionally two capital letters, but it could be anything. Yoursquo;ll want to avoid the two character prefixes already used by iOS, particularly NS and UI. Leave it blank for this project—you wonrsquo;t be creating any classes.
Click the Next button. Pick a location on your hard drive to save the new project and click Create.
Setting Project Properties
You now have an empty Xcode project; itrsquo;s time to start customizing it. Begin with the project settings by clicking on the project name (Surrealists) in the project navigator, as shown in the upper left of Figure 2-4. The editor area will display all of the settings for this project. Choose the Surrealist target from the pop-up menu, in the upper-left corner of the editor (see Figure 2-4), and then choose the General tab in the middle.
Figure 2-4. Target Settings
Scroll down the target settings until you find the Deployment Info section. Uncheck the Landscape Left and Landscape Right boxes in Device Orientation, so that only the Portrait orientation is checked.
To review, yoursquo;ve created an iPhone-only app project that runs exclusively in portrait orientation. Yoursquo;re now ready to design your interface.
Building an Interface
Click the Main.storyboard file in the project navigator. Xcodersquo;s Interface Builder editor appears in the edit area, as shown in Figure 2-5.
Figure 2-5. Interface Builder
Interface Builder is the secret sauce in Applersquo;s app kitchen. In a nutshell, itrsquo;s a tool that adds, configures, and interconnects objects within your app—without writing any code. You can define most of the visual elements of your app in Interface Builder. Interface Builder edits storyboard, xib, and (legacy) nib files.
Note Modern Interface Builder files have extensions of xib or storyboard. Legacy Interface Builder files have a nib (pronounced “nib”) extension, and yoursquo;ll still hear programmers refer to all of them generically as “nib” files. The nib acronym stands for Next Interface Builder, because the roots of Xcode, Interface Builder, and the Cocoa Touch framework stretch all the way back to Steve Jobrsquo;s “other” company, NeXT. Later in this book, yoursquo;ll see a lot of clas
剩余内容已隐藏,支付完成后下载完整资料
APP黄金时代!
在这一章,你将开始创建一个iOS应用来做一些事情。在早期,我们不会让它做特别多的事情,但足以派的上用场。接下来,你将要:
- 使用Xcode的xib来设计你的App
- 添加对象到你的App
- 将这些对象联系起来
- 自定义这些对象的内容
- 添加资源文件到你的App
- 使用storyboards来给它们建立联系
- 使用约束来控制视图布局
哇哦,你将不需要一行代码就可以创建一个App。这还不是最经典的,但是它将证明Xcode的灵活性。
这个即将诞生的App将会呈现一些有趣的事实关于20世纪的超现实女性。让我们开始吧!
设计
在你启动Xcode和开始编码之前,你需要制定一个计划。这就是一个应用开发的设计部分。贯穿你的整个项目生命周期,你可能会多次改善你的项目设计,但是在你开始设计之前,你需要基本点关于你希望你的应用长得什么样以及你需要他能做些什么。
你的设计可能会很顺利的完成,画出一个原型图甚至仅仅是在你的脑海里。那没关系,只要你有一个就行了。至少你需要拥有回答一些基础问题的能力。你的应用运行在哪一类设备上(iPhone/iPod,iPad,或者两者皆可)?你的应用是否支持横屏?用户将会看到什么?用户如何使用它?用户怎样与之交互?
图2-1给出了一个很糟糕的应用原型图。这个应用非常的简单,所以它不存在什么设计上的困难。这个超现实的App,它的屏幕显示了几幅著名女性的超现实画像。点击其中一幅画像,将会有1秒的时间去展示这幅作品以及一个滚动字符框,描述着这个这个艺术家的一生。这些将会明确你的设计和开发。
图2-1 超现实App的原型图
创建项目
第一步,你需要创建你的项目。在初始化页面单击新建项目按钮或者使用新建指令command N,通过图2-2让我们回顾一下我们的可用模板。
图2-2 iOS项目模板
你的设计给了你一个基本思路,就是你的应用该如何工作,我们需要在iOS项目模板中选择一个来开始我们的项目。你的App的设计并不适用于所有的模板,所以我们选择单视图应用这个模板,这个最简单的模板已经拥有了一个视图。再点击Next按钮。
下一步,我们需要填写这个项目的介绍(详见图2-3)。给这个项目命名为Surrealists,然后填写你的组织名和id。与你的设计选择保持一致,把设备选择从Universal改为为iPhone。
图2-3 设置项目描述
注意 iPhone开发与iPod开发一致(除非你的应用用到了只适用于iPhone的特色)。从现在开始,我们只谈论iPhone,但是请记住,这些内容同样适用于iPod。
这里还有一个类的配置设置。这个选项设置了一个非常短的字符串,这个字符串为你以后新添加到项目的所有类提供了一致的名字。它一般是两个大写字母,当然你也可以设置任何字符。你还需要注意这个前缀名避免与iOS系统名重名,比如NS和UI。如果你不想创建任何类,那就让它空着吧。
单击Next按钮,在你的设备上选择一个项目路径来存储新的项目并且单击Create。
配置项目参数
现在,你拥有了一个空白的Xcode项目;是时候去自定义我们的项目了。在项目导航器通过单击项目名字(Surrealists)开始项目设置,如图2-4左上角所示。编辑区域则会显示该项目的所有设置。在弹出菜单上选择Surrealists目标(如图2-4左上角),然后在中间选择General栏。
图2-4 目标设置
下拉目标设置直到你发现Deployment Info区域,在Device Orientation栏取消勾选Landscape Left和Landscape Right选框。确保只有Portrait orientation被勾选。
到现在,你已经创建了一个只运行在竖屏状态iPhone App工程,现在你可以准备设计你的界面了。
创建界面
点击项目导航器的Main.storyboard文件,Xcode的界面编辑器显示在编辑区域,如图2-5所示。
图2-5 界面编辑器
界面编辑器可以看作是苹果应用厨房的私藏酱料。简而言之,它是一个在你的App里添加,配置,互相连接对象的工具,无需任何代码。你可以在界面编辑器定义你的App的大部分可视元素。界面编辑器主要操作storyboard,xib,和(传统意义的xib) nib文件。
注意 现代的界面编辑器拥有xib和storyboard的扩展功能。传统的界面编辑器文件有一个.nib的扩展名,并且你仍然也会听到程序员通常称它们为nib文件。nib是Next Interface Builder的缩写,因为Xcode的权限,界面编辑器和Cocoa Touch框架包含了所有追溯到史蒂夫·乔布斯的“other” 公司——NeXT。在本书的剩余部分,你将会看到大量类的命名以“NS”开头,这是NeXTStep的缩写,而NeXTStep是NeXT公司的操作系统的名字。
界面编辑器展示这些对象在两个视图的两个文件里。在左边的是一些排列在垂直列表里的对象,称之为大纲。一些对象可以包含其他对象,就像文件夹可以包含其他文件夹一样,大纲也像这样。使用一种公开的三角形态来展现被包含的对象。
右边的视图称之为画板,你可以发现一些可视的对象在你的视图编辑器文件里。只有可见的对象(例如按钮、标签、图片等等)出现在画板里面。不可见的对象只会出现在大纲里面。如果一个对象在这两者中都出现了,无论你操作的是哪一个,它们都是同一个对象。
注意 你已经学习了面向对象编程语言,然后你知道了“对象”代表着什么,不要惊慌。从现在开始,你只需要把对象当作一个个乐高积木就好了:一个用来完成你App某一项工作的实例化类,并且可以和其他对象联系去完成一些更壮观的工作。现在你大可以怀着轻松的心情跳到第六章去学习对象的相关知识。
添加对象
你需要从对象库里面获取对象。选择View ➤ Utilities ➤ Show Object Library命令,同时在右边可见的功能区域切换到对象库(一个小方块),如图如图2-6所示。
图2-6 对象库
去添加一个对象到你的App,从对象库拖拽到界面编辑器。你的App需要一个导航控制器对象,然后拖动列表对象直到你找到Navigation Controller,在你明确所需查找的对象的情况下,可以在底部的搜索栏输入关键字查找相关对象。
图2-7 添加导航控制器
将导航控制器对象从对象库拖拽到画板,如图2-7所示,并且放置到空白区域的任意位置。这样,你已经添加了对象到你的App里。
删除和关联对象
这个库里面的导航控制器对象就相当于一个对象容器。一个导航控制器,顾名思义,用于管理用户在多界面的交互逻辑,每个界面又被一个单独的视图控制器管理。这个导航控制器作为我们应用启动的首页面,称之为root view controller。不用在意它里面的一些细节,你将会在第12章学到关于导航控制器的相关内容。
为了让你更加的方便,这个导航控制器在创建的同时,自带了一个根视图控制器用于显示。这个根视图控制器一般是一个表视图控制器。什么?你不需要一个表视图控制器。另外,你想要这个视图控制器使用你自己已有的视图控制器作为根视图控制器。
通过丢弃冗余的表视图控制器开始。只用表视图控制器连接导航控制器,如图2-8所示。按下delete键或者选择Edit ➤ Delete命令。
图2-8 删除表视图控制器
现在你需要连接你的新视图控制器到你项目的入口控制器。拖拽这个视图控制器,放到导航控制器的右边,见图2-9。
图2-9 指定初始化视图控制器
这个附在视图控制器上的箭头意味着该视图控制器作为你的App的初始化视图控制器。你想要让导航控制器成为第一个视图控制器,将箭头从单个视图控制器拖离,放置到导航控制器上,如图2-9所示。
最后一步是重新建立导航控制器的根视图控制器。在界面编辑器有几种方法来建立联系。我会给你演示最常见的两种。按住ctrl键,点击在导航控制器上同时拖拽到需要设置为根视图控制器的视图控制器上,如图2-10所示。
图2-10 设置根视图控制器
当你释放鼠标,一个选框弹出并列出了两个对象的所有可能的关联。点击root view controller连接,现在这个导航控制器将会将这个视图控制器作为App启动的初始页面。
之前说过要教你两种关联方式,第二种方法就是使用功能区的关联器。选择View ➤ Utilities ➤ Show Connections Inspector指令或者在功能面板选择箭头图标的按钮,见图2-11。
图2-11 使用关联器
开始使用关联器,首先选择一个对象。如上所示,选择导航控制器,这个关联器会展示这个对象的所有可能关联。找到名为root view controller的连接。每个连接的右边都有一个小圆圈,要设置连接,你只需要点击并且拖拽到你想要连接的对象。例中所示,要删除一个关联,只需要在关联区域点击“X”状按钮。
到目前为止,你已经创建了一个新的项目,这个项目模板包含了一个简单视图控制器。你添加了一个新的导航控制器对象到你的App。你把这个导航控制器作为你应用启动之后的初始控制器,并且你向他连接了一个空视图控制器。现在,是时候对这个空视图做点文章了。
往视图上面添加视图
现在我们已经感受到了这个项目的部分乐趣了:丰富你的应用内容。开始添加四个自定义按钮,显示在你的初始屏幕上。为了实现这个效果,你需要设置你的初始化视图对象。
这个视图控制器不是一个单独的对象,而是一个对象集。我之前说过,一些对象会包含一些对象。视图控制器和视图就是这样的对象。在界面编辑器中有两种方式选择视图对象,你可以发现在左边大纲里面的对象,选择它。另外的一些是“挖掘”你想要的对象。点击视图控制器的中心(图2-12的中心)。这样可以选择到视图控制器对象。再次点击然后你选择包含在这个对象里面的视图对象。如果那个对象包含了其他对象,再次点击它就会选择到它,如此往复。
图2-12 选择视图对象
现在你知道如何找到并选择内嵌的视图对象,是时候添加新的视图对象到它上面了。在对象库里面,找到按钮对象,在搜索框输入“button”会更加的快捷,选择按钮对象并把它拖拽到视图对象里,如图2-13。
图2-13 添加一个按钮对象
注意 被一个视图包含的所有视图对象称之为它的子视图。这个包含其它视图对象的称之为父视图。
重复这个步骤3次,然后在你的视图里面就有了四个按钮对象,就像图2-14所示。现在你需要重新布局这些按钮,让它们铺满整个屏幕。为了让你更容易布局,在这里给出一些指导。在父视图里面点击,不要选中任何按钮,然后选择Editor ➤ Add Vertical Guide命令。默认情况下,一个新的指导意见就出现在了视图中间,这也恰恰是你需要的。
图2-14 布局按钮
使用指导意见,布局这些按钮,可以让每个按钮铺满四分之一视图,开始拖拽这个按钮到视图的左下角的四分之一位置。这个视图会捕捉最贴近的指导布局。手动缩放按钮大小让它铺满四分之一大小的位置,如图2-14。这种做法并不算完美,在这章的剩余部分会教你如何微调它们。
当你做完这些工作,你就拥有了四个铺满屏幕的按钮(详见图2-15)。
图2-15 自定义按钮对象
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[29455],资料为PDF文档或Word文档,PDF文档可免费转换为Word
您可能感兴趣的文章
- 饮用水微生物群:一个全面的时空研究,以监测巴黎供水系统的水质外文翻译资料
- 步进电机控制和摩擦模型对复杂机械系统精确定位的影响外文翻译资料
- 具有温湿度控制的开式阴极PEM燃料电池性能的提升外文翻译资料
- 警报定时系统对驾驶员行为的影响:调查驾驶员信任的差异以及根据警报定时对警报的响应外文翻译资料
- 门禁系统的零知识认证解决方案外文翻译资料
- 车辆废气及室外环境中悬浮微粒中有机磷的含量—-个案研究外文翻译资料
- ZigBee协议对城市风力涡轮机的无线监控: 支持应用软件和传感器模块外文翻译资料
- ZigBee系统在医疗保健中提供位置信息和传感器数据传输的方案外文翻译资料
- 基于PLC的模糊控制器在污水处理系统中的应用外文翻译资料
- 光伏并联最大功率点跟踪系统独立应用程序外文翻译资料
