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

68

CHAPTER 3: Handling Basic Interaction

Bring It on Home

This chapter’s simple application introduced you to MVC, creating and connecting outlets and actions, implementing view controllers, and using application delegates. You learned how to trigger action methods when a button is tapped and saw how to change the text of a label at runtime. Although we built a simple application, the basic concepts we used are the same as those that underlie the use of all controls under iOS, not just buttons. In fact, the way we used buttons and labels in this chapter is pretty much the way that we will implement and interact with most of the standard controls under iOS.

It’s critical that you understand everything we did in this chapter and why we did it. If you don’t, go back and redo the parts that you don’t fully understand. This is important stuff! If you don’t make sure you understand everything now, you will only get more confused as we get into creating more complex interfaces later in this book.

In the next chapter, we’ll take a look at some of the other standard iOS controls. You’ll also learn how to use alerts to notify the user of important happenings and how to use action sheets to indicate that the user needs to make a choice before proceeding. When you feel you’re ready to proceed, give yourself a pat on the back for being such an awesome student, and head on over to the next chapter.

www.it-ebooks.info

Chapter 4

More User Interface Fun

In Chapter 3, we discussed MVC and built an application using it. You learned about outlets and actions, and used them to tie a button control to a text label. In this chapter, we’re going to build an application that will take your knowledge of controls to a whole new level.

We’ll implement an image view, a slider, two different text fields, a segmented control, a couple of switches, and an iOS button that looks more like, well, an iOS button. You’ll see how to set and retrieve the values of various controls. You’ll learn how to use action sheets to force the user to make a choice, and how to use alerts to give the user important feedback. You’ll also learn about control states and the use of stretchable images to make buttons look the way they should.

Because this chapter’s application uses so many different user interface items, we’re going to work a little differently than we did in the previous two chapters. We’ll break our application into pieces, implementing one piece at a time, and bouncing back and forth between Xcode and the iPhone simulator, testing each piece before we move on to the next. Dividing the process of building a complex interface into smaller chunks makes it much less intimidating, as well as more like the actual process you’ll go through when building your own applications. This code-compile-debug cycle makes up a large part of a software developer’s typical day.

A Screen Full of Controls

As we mentioned, the application we’re going to build in this chapter is a bit more complex than the one we created in Chapter 3. We’ll still use only a single view and controller, but as you can see in Figure 4–1, there’s a lot more going on in this one view.

D.Mark et al., Beginning iOS 5 Development

©Dave Mark, Jack Nutting, Jeff LaMarche 2011

www.it-ebooks.info

70

CHAPTER 4: More User Interface Fun

Figure 4–1. The Control Fun application, featuring text fields, labels, a slider, and several other stock iPhone controls

The logo at the top of the iPhone screen is an image view, and in this application, it does nothing more than display a static image. Below the logo are two text fields: one that allows the entry of alphanumeric text and one that allows only numbers. Below the text fields is a slider. As the user moves the slider, the value of the label next to it will change so that it always reflects the slider’s current value.

Below the slider is a segmented control and two switches. The segmented control will toggle between two different types of controls in the space below it. When the application first launches, two switches will appear below the segmented control. Changing the value of either switch will cause the other one to change its value to match. Now, this isn’t something you would likely do in a real application, but it does demonstrate how to change the value of a control programmatically and how Cocoa Touch animates certain actions without you needing to do any work.

Figure 4–2 shows what happens when the user taps the segmented control. The switches disappear and are replaced by a button. When the Do Something button is pressed, an action sheet pops up, asking if the user really meant to tap the button (see Figure 4–3). This is the standard way of responding to input that is potentially dangerous or that could have significant repercussions, since it gives the user a chance to stop potential badness from happening. If Yes, I’m Sure! is selected, the application will put up an alert, letting the user know that everything is OK (see Figure 4–4).

www.it-ebooks.info

CHAPTER 4: More User Interface Fun

71

Figure 4–2. Tapping the segmented controller on the left side causes a pair of switches to be displayed. Tapping the right side causes a button to be displayed.

Figure 4–3. Our application uses an action sheet to solicit a response from the user.

www.it-ebooks.info

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