Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 3: Handling Basic Interaction

59

and Xcode will connect this button to the existing action method. That will cause this button, when tapped, to trigger the same action method as the other button.

Figure 3–10. Dragging to an existing action to whitespace will connect the button to an existing action.

Note that this will work even if you control-drag to connect your button to a method in your implementation file. In other words, you can control-drag from your new button to the buttonPressed declaration in BIDViewController.h or to the buttonPressed method implementation in BIDViewController.m. Xcode 4 sure am smart!

Adding the Label and Outlet

In the object library, type Label into the search field to find the Label user interface item (see Figure 3–11). Drag the Label to your user interface, somewhere above the two buttons you placed earlier. After placing it, use the resize handles the stretch the label from the left margin to the right margin. That should give it plenty of room for the text we’ll be displaying to the user.

Figure 3–11. The label as it appears in the object library

Labels, by default, are left-justified, but we want this one to be centered. Select View Utilities Show Attributes Inspector (or press 4) to bring up the attributes inspector (see Figure 3–12). Make sure the label is selected, and then look in the attributes inspector for the Alignment buttons. Select the middle Alignment button to center the label’s text.

www.it-ebooks.info

60

CHAPTER 3: Handling Basic Interaction

Figure 3–12. The attribute inspector for the label

Before the user taps a button, we don’t want the label to say anything, so double-click the label (so the text is selected) and press the delete button on your keyboard. That will delete the text currently assigned to the label. Hit return to commit your changes. Even though you won’t be able to see the label when it’s not selected, don’t worry—it’s still there.

TIP: If you have invisible user interface elements, like empty labels, and want to be able to see

where they are, select Canvas from the Assistant Editor menu, and then from the submenu that

pops up, turn on Show Bounds Rectangles.

All that’s left is to create an outlet for the label. We do this exactly the way we created and connected actions earlier. Make sure the assistant editor is open and displaying BIDViewController.h. If you need to switch files, use the popup above the assistant editor.

www.it-ebooks.info

CHAPTER 3: Handling Basic Interaction

61

Next, select the label in Interface Builder and control-drag from the label to the header file. Drag until your cursor is right above the existing action method. When you see something like Figure 3–13, let go of the mouse button, and you’ll see the popup window again (shown earlier in Figure 3–8).

Figure 3–13. Control-dragging to create an outlet

We want to create an outlet, so leave the Connection at the default type of Outlet. We want to choose a descriptive name for this outlet so we’ll remember what it is used for when we’re working on our code. Type statusText into the Name field. Leave the Type field set to UILabel. The final field, labeled Storage, can be left at the default value.

Hit return to commit your changes, and Xcode will insert the outlet property into your code. Your controller class’s header file should now look like this:

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *statusText; - (IBAction)buttonPressed:(id)sender;

@end

Now, we have an outlet, and Xcode has automagically connected the label to our outlet. This means that if we make any changes to statusText in code, those changes will affect the label on our user interface. If we set the text property on statusText, for example, it will change what text is displayed to the user.

www.it-ebooks.info

62

CHAPTER 3: Handling Basic Interaction

Single-click BIDViewController.m in the project navigator to look at the implementation of our controller. There, you’ll see that Xcode has inserted a @synthesize statement for us for the property that it created. It also did something else. Remember that method that we left in our code when we deleted the boilerplate methods? Look at it now:

- (void)viewDidUnload {

[self setStatusText:nil]; [super viewDidUnload];

//Release any retained subviews of the main view.

//e.g. self.myOutlet = nil;

}

See that line of code above the call to super? Xcode added that automatically as well. When our view is unloaded, we need to “let go” of all of our outlets; otherwise, their memory can’t be freed. Assigning a value of nil to the outlet does just that—it allows the previous value to be released from memory.

Essentially, control-dragging to create the outlet did everything we needed to set up the outlet for use.

AUTOMATIC REFERENCE COUNTING

If you’re already familiar with Objective-C, or if you’ve read earlier versions of this book, you might have noticed that we don’t have a dealloc method. We’re not releasing our instance variables!

Warning! Warning! Danger, Will Robinson!

Actually, Will, you can relax. We’re quite OK. There’s no danger at all—really.

It’s no longer necessary to release objects. Well, that’s not entirely true. It is necessary, but the LLVM 3.0 compiler that Apple started shipping with iOS 5 is so smart that it will release objects for us, using a new feature called Automatic Reference Counting, or ARC, to do the heavy lifting. That means no more dealloc methods, and no more worrying about calling release or autorelease.

ARC applies to only Objective-C objects, not to Core Foundation objects or to memory allocated with malloc() and the like, and there are some caveats and gotchas that can trip you up, but for the most part, worrying about memory management is a thing of the past.

To learn more about ARC, check out the ARC release notes at this URL:

https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN -TransitioningToARC/

ARC is very cool, but it’s not magic. You should still understand the basic rules of memory management in Objective-C to avoid getting in trouble with ARC. To brush up on the Objective-C memory management contract, read Apple’s Memory Management Programming Guide at this URL:

https://developer.apple.com/library/ios/#documentation/Cocoa/Concep

tual/MemoryMgmt/

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]