【译】Objectively Speaking 2: A Crash Course in Objective-C for iOS 6
In this Objective-C tutorial, you will create a simple movie quotes quiz app. Along the way, you’ll become acquainted with a number of aspects of Objective-C, including:(在这章节中,你将会了解到OC的一些特性。包括了:)
- mutable arrays
- property lists
- classes
- methods
- @property
- protocols and delegates
- additional string functions (string额外的功能)
- and much more!
You will notice that your project contains the following key files:
- QuoteQuizAppDelegate.h
- QuoteQuizAppDelegate.m
- QuoteQuizViewController.h
- QuoteQuizViewController.m and
- MainStoryboard.storyboard
QuoteQuizAppDelegate contains some code gets called when the app first starts up, or other “events of interest” occur like the app entering the background. For this tutorial, that’s all you need to know. Here are some brief explanations of the other files, with which you will be working directly:
- MainStoryboard.storyboard contains the visual layout of your app. This is where you use a visual designer to create the screens for your app.
- QuoteQuizViewController.m is the view controller class for your scene. This file contains your app’s Objective-C code. The storyboard above links the scene to this class in the storyboard; Xcode did this automatically when you created your project. Any scenes that you add to your app will need to be linked manually; you’ll cover this later in the tutorial.
- QuoteQuizViewController.h is the header file for your view controller. This is where you declare public properties and methods that need be accessed from outside the class.
Okay, enough theory — the best way to learn Objective-C is to roll up your sleeves and get coding!(卷起你的袖子准备开始写代码)
Showing the Tips
Now that all of the supporting modules have been created, you’ll need to create a view controller to show the requested tips to the player.
To do this, go to File\New File…, choose the iOS\Cocoa Touch Class\Objective-C class template, and click Next. Name the new class QuizTipViewController and make it a subclass of UIViewController. Finally, click Next then Create.
Now you need to add a protocol to QuizTipViewController.h so that this class becomes a delegate.
Add the following code to QuizTipViewController.h just before @interface:
@protocol QuizTipViewControllerDelegate; |
A protocol allows classes to share similarly defined behavior. This is helpful when classes are not related to each other, but still behave in similar ways. In this way, classes do not need to “know” about one another, which reduces dependencies throughout your code.
Notice that the protocol has the word “delegate” in it. What’s a delegate?
A delegate is an object that performs work on behalf of another object. For example, a foreman delegates the construction of a sidewalk to some of his workers in the same way an object may delegate the processing of information to another object. In this case, the class will notify another object that an event has occurred.
By using delegates with protocols, the delegator need not be concerned with the actual objects that are doing the work — so long as the work gets done! :]
Add the following code to QuizTipViewController.h between @interface and @end:
@property (nonatomic, assign) id <QuizTipViewControllerDelegate> delegate; |
When the user presses the “Done” button, QuizTipViewController will notify its delegate that the user is finished and the tip view should be dismissed. In this case, the delegate is QuizViewController which also is the presenter of QuizTipViewController.
Once QuizViewController is told that the “show is over”, it will dismiss QuizTipViewController.
Add the following line of code to QuizTipViewController.h just before @end:
- (IBAction)doneAction:(id)sender; |
You’re going to add a button that says “Done” on this view controller, and here you’re declaring the method that you’ll set up to be called when the button is tapped.
Finally, declare the rest of your protocol. While protocols can be contained in their own file, it is also common for developers to define them in the header file of the related class.
Add the following code to QuizTipViewController.h just after @end:
@protocol QuizTipViewControllerDelegate |
Here you say that any class that implements this protocol must implement one method,quizTipDidFinish:.
Switch to QuizTipViewController.m. At the end of viewDidLoad, add the following code:
self.tipView.text = self.tipText; |
This will set the tipText field to the tip that was set in the class variable.
Next, add the following method to QuizTipViewController.m, just before the @end declaration:
- (IBAction)doneAction:(id)sender |
Here you tell call the quizTipDidFinish on the delegate method when the button is tapped.
Next, let’s make the QuoteQuizViewController implement the delegate.
Switch over to QuoteQuizViewController.h and add the import line for QuizTipViewController.hdirectly below the import statement for UIKit. Also add <QuizTipViewControllerDelegate> to the end of the @interface declaration to mark it as implementing the protocol, as shown below:
#import <UIKit/UIKit.h> |
Open QuoteQuizViewController.m and create quizTipDidMethod — as defined by the protocol — by adding the code below just before the final @end:
- (void)quizTipDidFinish:(QuizTipViewController *) controller {
|
So when the user taps the done button, this delegate method gets called, and all you do is dismiss the view controller. You could do some more fancy work here if you wanted, like saving data.
Now it’s time to add this new view controller to your user interface. Open theMainStoryboard.storyboard and drag in a new view controller, as shown in the screenshot below:

Select the new view controller and switch to the Identity Inspector (the third tab in). Under the Custom Class section, set the class field to be QuizTipViewController, as shown below:

Drag a label onto the view and place it near the top of the screen. Double-click the label and modify the label’s text to be “Movie tip (3 tips only)“. Center the label in the middle of the view.
Now drag in a textview under the label. Set the width to be 280 points, and the height to be 136 points.
Place a button underneath the textview, and make it the same width as the textview. Modify the text of the button to read “Done”.
Your interface should now look like the following:

Right-click the Quiz Tip View Controller Scene, drag it to the text view, and then select the tipViewoutlet. Next, right-click the Done button, drag it to the Quote Tip View Controller, and select doneAction.
Now it is time to link up the two view controllers using a segue. First you will need a tip button — this will be the button that triggers the app to show the quiz tip view.
Scroll over to the previous view controller and select the “Next” button. Copy and paste the Next button, and drag this new button to the left side of the view controller. Modify the button’s text to read Tip.

Since you copied the button, you also copied the actions associated with it. Right click the button, and click the ‘X’ button next to the action associated with “Touch Up Inside” action, to remove the action, as shown below:

We could have just dragged in a new button, but this is a common problem to run across so I wanted to show you it.
Finally, right-click Quote Quiz View Controller, drag over to the tip button, then select the infoButtonoutlet.
【译】Objectively Speaking 2: A Crash Course in Objective-C for iOS 6的更多相关文章
- 分析iOS Crash文件,使用命令符号化iOS Crash文件
TBMainClient.ipa改名为TBMainClient.zip并解压得到TBMainClient.app 然后将TBMainClient.app TBMainClient.app.d ...
- iPhone Tutorials
http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...
- 【译】Experienced programmers but new to Objective-C(一)
注:这是raywenderlich博客上的一个系列文章.是写给从其他语言转到OC语言上的程序员的,一共5节.最近打算学习一下,并且把一些重要的知识点摘抄并且尝试翻译一下,第一次翻译,有些原文如果不知道 ...
- iOS --------Crash 分析(一)
iOS Crash 分析(文一)- 开始 1. 名词解释 1. UUID 一个字符串,在iOS上每个可执行文件或库文件都包含至少一个UUID.目的是为了唯一识别这个文件. 2. dwarfdump 苹 ...
- 腾讯bugly 的crash 上报和umeng的比较
说到crash上传工具,大家肯定会第一时间想到umeng,不错,umeng 是最早推出 crash 上报的工具之一,在刚推出来的时候,特别受到ios开发人员的喜爱. 因为个时候,内存是手动管理的,很容 ...
- Android & iOS 第三方 Crash ANR 捕捉上传
1. Bugly 地址:http://bugly.qq.com/ 提供:专业的Crash(崩溃).Android ANR(application not response).iOS卡顿监控和解决方案. ...
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
作者:林蓝东 最近的一个手机 QQ 版本发出去后收到比较多关于 CoreMotion 的 crash 上报,案发现场如下: 但是看看这个堆栈发现它完全不按照套路出牌啊! 乍一看是挂在 CoreMoti ...
- iOS 严重问题解释(crash)
问题1:Exception Type: 00000020 Exception Codes: 0x000000008badf00d Exception Note: SIMULATED (this is ...
- Crash日志分析
从Crash文件出发解决bug的一般步骤,分三步: a, 获取设备上的崩溃日志. b, 分析崩溃日志,找到报错位置(定位到函数和代码行数). c, 打开代码,改bug. 1, 获取设备日志 1. 在可 ...
随机推荐
- linux_之sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以 将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- MVC+MQ+WinServices+Lucene.Net
MVC+MQ+WinServices+Lucene.Net Demo 前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精 ...
- 动态类(Dynamic)应用
动态类(Dynamic)应用 背景: 在Coding中有时候会遇到一些需要解析的数据,可是数据的字段数量和名称未统一,我们没法定义实体类来对应.那么我们就会想到通过C#的dynamic动态类来实现,如 ...
- MySQL 升级方法指南大全
原文:MySQL 升级方法指南大全 通常,从一个发布版本升级到另一个版本时,我们建议按照顺序来升级版本.例如,想要升级 MySQL 3.23 时,先升级到 MySQL 4.0,而不是直接升级到 MyS ...
- 登录记住账号和密码小Demo
读取 // 1.读取沙盒中plist文件 // 1.1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 1.2.拼接Documents路径 NSStrin ...
- JavaScript随记汇总
1.<script>标签嵌套,浏览器无法正常解析的问题: 百度知道回答 <script>FTAPI_slotid = 1007894;FTAPI_sync = true< ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- c语言中实现从0-1的随机数输出
原文:c语言中实现从0-1的随机数输出 今天晚上同学问了一个巨简单的问题,问我怎么用c语言输出0-1的随机数,可别说,一时之间还想不出来.在写的过程中发现,直接调用random函数还不能实现,用以下方 ...
- 使用IntelliLock加密授权你的.Net程序
原文:使用IntelliLock加密授权你的.Net程序 转自:http://www.nsoff.com/post/2012/05/23/%E4%BD%BF%E7%94%A8IntelliLock%E ...
- RESTful API的设计原则
好RESTful API的设计原则 说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间, ...