Cocoa Application Tutorial - Apple (2009).pdf

(2999 KB) Pobierz
Cocoa Application Tutorial
Cocoa Application Tutorial
General
2009-08-03
727161710.002.png
Apple Inc.
© 2009 Apple Inc.
All rights reserved.
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
exclusionmaynotapplytoyou.Thiswarrantygives
youspecificlegalrights,andyoumayalsohave
otherrightswhichvaryfromstatetostate.
Nopartofthispublicationmaybereproduced,
stored in a retrieval system, or transmitted, in
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
thedocumentationcontainsApple’scopyright
notice.
The Apple logo is a trademark of Apple Inc.
No licenses, express or implied, are granted
withrespecttoanyofthetechnologydescribed
in this document. Apple retains all intellectual
propertyrightsassociatedwiththetechnology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
.Mac is a registered service mark of Apple Inc.
Apple,theApplelogo,AppleScript,AppleScript
Studio, Carbon, Cocoa, eMac, Finder, iTunes,
Mac, Mac OS, Macintosh, Objective-C, Quartz,
and Xcode are trademarks of Apple Inc.,
registered in the United States and other
countries.
Smalltalk-80 is a trademark of ParcPlace
Systems.
UNIX is a registered trademark of The Open
Group
EventhoughApplehasreviewedthisdocument,
APPLEMAKESNOWARRANTYORREPRESENTATION,
EITHEREXPRESSORIMPLIED,WITHRESPECTTO
THISDOCUMENT,ITSQUALITY,ACCURACY,
MERCHANTABILITY,ORFITNESSFORAPARTICULAR
PURPOSE.ASARESULT,THISDOCUMENTIS
PROVIDED“ASIS,”ANDYOU,THEREADER,ARE
ASSUMINGTHEENTIRERISKASTOITSQUALITY
ANDACCURACY.
INNOEVENTWILLAPPLEBELIABLEFORDIRECT,
INDIRECT,SPECIAL,INCIDENTAL,OR
CONSEQUENTIALDAMAGESRESULTINGFROMANY
DEFECTORINACCURACYINTHISDOCUMENT,even
ifadvisedofthepossibilityofsuchdamages.
THEWARRANTYANDREMEDIESSETFORTHABOVE
AREEXCLUSIVEANDINLIEUOFALLOTHERS,ORAL
ORWRITTEN,EXPRESSORIMPLIED.NoApple
727161710.003.png
Contents
Introduction IntroductiontoCocoaApplicationTutorial 9
Organization of This Document 9
Goals for Learning 9
Prerequisites 10
See Also 10
Chapter1 TheEssenceofCocoa 11
What Is Cocoa? 11
Classes and Objects 11
The MVC Design Pattern 12
Model Objects 12
View Objects 12
Controller Objects 13
Hybrid Models 13
The Currency Converter Application 13
Chapter2 CreatingaProjectinXcode 15
Open Xcode 15
Make a New Project 15
Choose the New Project Command 15
Choose a Project Type 16
The Xcode Project Interface 17
What’s Next? 18
Chapter3 DefiningtheModel 19
Specify the Model Class 19
Declare the Model Interface 20
Declare Instance Variables 20
Declared Properties and Accessor Methods 20
Declare the Model Method: convertCurrency 21
Implementing the Model 22
Define the convertCurrency Method 22
What’s Next? 22
Chapter4 DefiningtheView:BuildingtheUserInterface 23
User Interface Elements and Interface Builder 23
What Is a Nib File? 23
2009-08-03 | © 2009 Apple Inc. All Rights Reserved.
3
727161710.004.png
CONTENTS
Windows and Menus in Cocoa 24
Creating the Currency Converter Window 25
Create the Window 25
Resize the Window 25
Changing the Title of the Window 27
Add the Text Fields 27
Assign Labels to the Fields 29
Change the Text Field Attributes 31
Configure a Button 31
Add a Separator to Group Controls 32
Refining the Layout and View Functionality 33
Aligning Objects in a Window 33
Finalize the Window Size 34
Enable Tabbing Between Text Fields 35
Set the First Responder Text Field 36
Test the Interface 37
What’s Next? 38
Chapter5 BridgingtheModelandView:TheController 39
Paths for Object Communication: Outlets, Targets, and Actions 39
Outlets 39
Target/Action in Interface Builder 40
Which Direction to Connect? 41
Defining the Controller Class 42
Defining the Outlets for the ConverterController Class 42
Declare the Controller Method 42
Interconnecting the Controller with the View 43
Add the ConverterController Class to Your Nib File 43
Connect the ConverterController Instance to the Text Fields 45
Connect the Convert Button to the Appropriate Methods 46
Check Your Work 46
Connecting the Controller to the Model 47
Garbage Collection 48
What’s Next? 49
Chapter6 BuildingandRunningYourApplication 51
Build the Application 51
Extra: Check Out the Documentation 51
Run the Application 52
Correct Build Errors 52
Great Job! 52
What’s Next? 53
4
2009-08-03 | © 2009 Apple Inc. All Rights Reserved.
727161710.005.png
CONTENTS
Chapter7 ConfiguringYourApplication 55
The Info.plist File 55
Basic Project Attributes 55
Specify the Identifier, Version, and Copyright Information 58
Create an Icon File 61
What’s Next? 65
Chapter8 ExpandingontheBasics 67
For Free with Cocoa 67
Application and Window Behavior 67
Controls and Text 67
Menu Commands 68
Document Management 68
File Management 68
Communicating with Other Applications 69
Custom Drawing and Animation 69
Internationalization 69
Editing Support 69
Printing 69
Help 70
Plug-in Architecture 70
Turbo Coding with Xcode 70
Project Find 70
Code Sense and Code Completion 70
Integrated Documentation Viewing 70
Indentation 71
Delimiter Checking 71
Emacs Bindings 71
AppendixA Objective-CQuickReferenceGuide 73
Messages and Method Implementations 73
Declarations 74
DocumentRevisionHistory 75
2009-08-03 | © 2009 Apple Inc. All Rights Reserved.
5
727161710.001.png
Zgłoś jeśli naruszono regulamin