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

72

CHAPTER 4: More User Interface Fun

Figure 4–4. Alerts are used to notify the user when important things happen. We use one here to confirm that everything went OK.

Active, Static, and Passive Controls

Interface controls are in used in three basic modes: active, static (or inactive), and passive. The buttons that we used in the previous chapter are classic examples of active controls. You push them, and something happens—usually, a piece of code that you wrote fires.

Although many of the controls that you will use will directly trigger action methods, not all controls will. The image view that we’ll be implementing in this chapter is a good example of a control being used statically. Even though a UIImageView can be configured to trigger action methods, in our application, the image view is passive—the user cannot do anything with it. Text fields and image controls are often used in this manner.

Some controls can work in a passive mode, simply holding on to a value that the user has entered until you’re ready for it. These controls don’t trigger action methods, but the user can interact with them and change their values. A classic example of a passive control is a text field on a web page. Although it’s possible to create validation code that fires when the user tabs out of a field, the vast majority of web page text fields are simply containers for data that’s submitted to the server when you click the submit

www.it-ebooks.info

CHAPTER 4: More User Interface Fun

73

button. The text fields themselves usually don’t cause any code to fire, but when the submit button is clicked, the text field’s data goes along for the ride.

On an iOS device, most of the available controls can be used in all three modes, and nearly all of them can function in more than one mode, depending on your needs. All iOS controls are subclasses of UIControl and, because of that, are capable of triggering action methods. Many controls can be used passively, and all of them can be made inactive or invisible. For example, using one control might trigger another inactive control to become active. However, some controls, such as buttons, really don’t serve much purpose unless they are used in an active manner to trigger code.

There are some behavioral differences between controls on iOS and those on your Mac. Here are a few examples:

Because of the multitouch interface, all iOS controls can trigger multiple actions depending on how they are touched. The user might trigger a different action with a finger swipe across the control than with just a tap.

You could have one action fire when the user presses down on a button and a separate action fire when the finger is lifted off the button.

You could have a single control call multiple action methods on a single event. You could have two different action methods fire on the touch up inside event, meaning that both methods would be called when the user’s finger is lifted after touching that button.

NOTE: Although controls can trigger multiple methods on iOS, the vast majority of the time, you’re probably better off implementing a single action method that does what you need for a

particular use of a control. Though you won’t usually need this capability, it’s good to keep it in mind when working in Interface Builder. Connecting an event to an action in Interface Builder does not disconnect a previously connected action from the same control! This can lead to

surprising misbehaviors in your app, where a control will trigger multiple action methods. Keep an eye open when retargeting an event in Interface Builder, and make sure to remove old actions

before connecting to new ones.

Another major difference between iOS and the Mac stems from the fact that, normally, iOS devices do not have a physical keyboard. The standard iOS software keyboard is actually just a view filled with a series of button controls that are managed for you by the system. Your code will likely never directly interact with the iOS keyboard.

Creating the Application

Let’s get started. Fire up Xcode if it’s not already open, and create a new project called Control Fun. We’re going to use the Single View Application template again, so create your project just as you did in the previous two chapters.

www.it-ebooks.info

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