Xcode can also help you find things in your own project. The submenu accessed by choosing EditOFind provides several options for finding text in your own project.
You will find that, as your classes get bigger, sometimes you’ll want to find a single symbol or all occurrences of a symbol in a file or class. You can easily do that by choosing EditOFindOFind or pressing ^+F, which opens a Find toolbar to help you search the file in the Editor window. In Figure 7-5, for example, I typed viewDidLoad in the Find toolbar, and Xcode found all the instances of viewDidLoad in that file and highlighted them for me.
P ProlocCd MdrMH
- 5.11 ГеЬии ‘ й -
R. rftiijit Л Fllei ▼ KetumMe Го Claim
"Zj Bf штМгТп AppIV irgarr h
М KetomMetaAi>pUe4egat<.m W| RcLuuiUeTaV1cwCunLiuller. li _и] Rem m WpTn V1 ewfn nrm II» r. m
► ij_] Ottier Source s
* „ Reiuuriei
LlenimMpTaVlenCnnrrollrr. vl *] MainWimloA. xib | ReluinMeTu-infe. pliU •j Phnnr png J*] ReturnMeTo >con. pr-q
► C^FMmewortu
* Prwfiictt
► ©Taroeti
И Executable! * Find Result*
► LJi BoiAmarki
► t. SCM
Ф Crajprt Symbelt
► Ij^gj Implementation File i
► Ш Interface Buiidtf Filts
Ж
Figure 7-4:
Right-click UI
Application Delegate.
P ci u rriMe To ft ppDe i&n аге. h – Rciuinuefo
1ч О
Q* Suing Mantling Searrh * Cndf О
KeturnHe 1 oAppOeieiMte. li
Cut
Copy
Paste
•leport – L
► j^iJlionBelegalo
Sinter i Jt.( UlWinri Returil
«Property
Gprnprrty (—
TeuCnntrnller *inewt т
Balance
Re-Indent Selection Shift Selection Right Shift Selection 1 eft
Refaetor…
Next Completion Completion List Select Next Placeholder
Edit All In bcope
Code Folding Menage Bubble*;
Open As
Open in Separate editor Reveal Jn Tinder Reveal Jn Croup Tree Add to Bookmarks
Find in Project ►
Find Text in Documentation Jump to Definition Search in Spotlight
Cet Info
~ Make New Mail Note
Jr Add to ITunes as a Spoken Track
Figure 7-5:
Finding
View DidLoad in a file.
You can also use Find to go through your whole project by choosing EditOFindOFind in Project or by pressing ^+Shift+F. I pressed ^+Shift+F, which opened the window shown in Figure 7-6. I typed
ReturnMeToViewController, and then in the drop-down menu, I selected In Project. You can specify in what sets of files (open project files, and so on) you want to search. (A great feature for tracking down something in your code — you’re sure to use it often.)
If you select a line in the top pane, as you can see in Figure 7-6, the file in which that instance occurs is opened in the bottom pane and the reference highlighted.
AddINg Outlets to the View Controller
Now that you have some idea of how to use the Xcode editor, it’s time to write some code. Before taking you on our editor tour, I mentioned that one of the things I needed to do was add outlets to my ReturnMeTo application. That’s what you’re going to do now — add outlets to the
ReturnMeToViewController. Here’s how:
1. Go to the Xcode project window and, in the Groups & Files pane, click the triangle next to Classes to expand the folder.
2. From the Classes folder, select ReturnMeToViewController. h — the header file for ReturnMeToViewController.
The contents of the file appear in the main display pane of the Xcode editor, as shown in Figure 7-7 (Of course yours won’t have all of that code in it yet — you’ll be entering it in Step 4.)
Figure 7-7:
ReturnMe ToView Controller. h.
3. Look for the following lines of code in the header:
#import <UIKit/UIKit. h>
@interface ReturnMeToViewController: UIViewController{ }
@end
Got it? Great.
4. Type the following four lines of code between UIViewController{ and @end (the curly brace you see below the last IBOutlet and first @property statements will already be there).
IBOutlet UITextField *textField;
IBOutlet UILabel *label; }
@property (nonatomic, retain) UITextField *textField; @property (nonatomic, retain) UILabel *label;
When you’re done typing, your code should look exactly like Figure 7-7.
The first two lines of code here declare the outlets, which will automatically be initialized with a pointer to the text field (textField) and label objects (label) when the application is launched. But while this will happen automatically, it won’t automatically happen automatically. I have to help it out a bit.
In procedural programming, variables are generally fair game for all. But in object-oriented programming, a class’s instance variables are tucked away inside an object and shouldn’t be accessed directly. The only way
For them to be initialized is for you to create what are called accessor methods, which allow the specific instance variable of an object to be read and (if you want) updated. Creating accessor methods is a two-step process that begins with a @property declaration, which tells the compiler that there are accessor methods.
And that is what I did above; I coded corresponding @property declarations for each IBOutlet declaration. You’ll notice there are some arguments to the @property declaration. These specify how the accessor methods are to behave — I explain exactly what that means in the next section. For now, just know that you need to add them.
5. Go back to the Classes folder in the Groups & Files listing and select
ReturnMeToViewController. m — the implementation file for ReturnMeToViewController.
6. Look for the following lines of code in the implementation file:
#import "ReturnMeToViewController. h"
@implementation ReturnMeToViewController They’re pretty much right at the top.
7. Type the following two lines of code after @implementation ReturnMeToViewController and before anything else.
©synthesize textField; ©synthesize label;
When you’re done, your code should like what you see in Figure 7-8.
While the @property declaration tells the compiler that there are accessor methods, they still have to be created. In the good-old days, you had to code these accessor methods yourself and, in a large program, it got to be very tedious. Fortunately, Objective-C will create these accessor methods for you whenever you include an @synthesize statement for a property.
That is what you did above. The two @synthesize statements tell the compiler to create two accessor methods for you — one for each @property declaration.
8. Scroll down the code for ReturnMeToViewController. m until you reach the following lines:
- (void)dealloc {
[super dealloc];
}
You can use to find something in a single file, as opposed to Shift+^+F, which finds it in all project files.
Figure 7-8:
Completing the addition of the accessors.
9. Enter the following two lines of code between the - (void)dealloc { and [super dealloc]; lines:
[textField release]; [label release];
The new code should look like what you see in Figure 7-9.
Those of you who remember my obsession with memory management from previous chapters will recognize release as a tool for freeing up no-longer-needed memory commitments. (Those of you who have not yet heard my memory-management stump speech will get a chance to hear it later in this chapter.)
That’s it. You’ve added outlets to your view controller. Step back and admire your handiwork. Then move on to the next section and see how the little snippets of code you added above to your ReturnMeToViewController. m and ReturnMeToViewController. h files tie in with the basic principles of programming using the Objective-C language.
|
■ Re 1 ur n MrTnVi i-wConl roller
|
- RrturnMrTn
|
|
Simulator * i. l | Debug
|
Tfc т m
|
Vs
|
О
|
I’Q – «ring Matching
|
|
Orel view
|
Aitiun Eieakfiolntv
|
Uil-d and Run
|
Taifn Inlia
|
Search
|
Cruupi& Fries
Т ^ RrturnMrTn TQJ unit;
ReluiiiMcToAupDelegjte. il _ Rrr» rnMrTnAppDrIf garr m м, Ketu гпМе Г OVu-wCo’itroiler. h ы RcturnMeTuVmnCunliDller. ni ► OS Other 5аигг*л * Qj Resources
•.’ RcluiiiMeToV№inCiHiliuller. j(ib
RfiumMt-TaVlrwrnmrnllM. m
. с. I. • a
T*uprr ЛмПлг)[
■I >
-(UUCL)textl leldihoutdReturn: шло"1 jeld «Hlflextl ield (
С
Ь
J RrturnMrTn Irnn png » Frameworks H IPmdusts
► Targrts
• Lxecutitiles
* 4, Find Results
P и ftnali marts
► " SCM
M
Figure 7-9:
Doing a little memory management.