Tải bản đầy đủ - 0 (trang)
Chapter 6. Saving, Editing, and Sorting Data: Everyone’s an editor...

Chapter 6. Saving, Editing, and Sorting Data: Everyone’s an editor...

Tải bản đầy đủ - 0trang

sam’s new drink



Sam is ready to add a A new drink at the Lounge

Red-Headed School Girl...

Sam went to try DrinkMixer with the new add

view and ran into problems right away.



Sam was

clicking around,

ready to add

his new drink.

The directions

field is hidden

under the

keyboard.



You can’t see the directions at all, and part of

the ingredients information is covered up.



We have a problem with our

view, since we can’t get to

some of the fields.



252   Chapter 6

www.it-ebooks.info



Sam, the

bartender



saving, editing, and sorting data



...but the keyboard is in the way

We’re back to the keyboard problem we saw earlier with InstaEmail.

When Sam taps on a control, it gets focus (becomes the first

responder) and asks iOS to show the keyboard. Generally, that’s a

good thing. However...



When S

Drink naam taps in the

keyboard me field, the

supposed appears like it

to—that

’s

’s good.



And th

completee keyboard

Directio ly covers the

ns field!



We had a similar problem in

InstaEmail where the user

couldn’t get to the controls

under the keyboard.

p

ry to tafield

t

n

e

v

e

He can e Ingredients he

into th pe in some of t ns

and ty nts...but he ru

ingredie he keyboard.

under t



How did we deal with the keyboard last time? Will that work this time?

What do you want the view to do when the keyboard appears?



you are here 4  253

www.it-ebooks.info



scroll view up close



How did we deal with the keyboard last time? Will that work this time?

What do you want the view to do when the keyboard appears?



Resigning first responder worked last time. In DrinkMixer, it would be fine for the name field, but

what about the directions and the ingredients fields? As soon as the keyboard comes up, they’re

covered. The user has a smaller screen to work with once the keyboard shows up—we need to set up

the view to scroll things in when the user needs them. We can do this with a UIScrollView.



UIScrollView Up Close

UIScrollView is just like the basic UIView we’ve been using except that it can handle having items (like

buttons, text fields, etc.) be off the screen and then scrolling them into view. The scroll view draws and

manages a scroll bar, panning and zooming, and controlling which part of the content view is displayed.

It does all of this by knowing how big the area it needs to show is (called the contentSize) and how much

space it has to show it in (the frame). UIScrollView can figure out everything else from there.



Controls

(buttons, etc.)



Content view



The conte

to be just nbtu doesn’t have

text fields; ttons and

work well witUIScrollViews

h images, too

.



a

The scroll view acts like econtent

th

on

magnifying lens

ion is

view, so that only a port

.

er

visible to the us



UIScrollView ha

support for zooms built-in

panning around t ing and

view—you just ne he content

how big the conted to tell it

ent is.

Remember, in CocoaTouch, components are subclasses of UIView. All a scroll view needs to care

about are the subviews it has to manage. It doesn’t matter if it’s one huge UIImageView that shows a

big image you can pan around, or if it’s lots of text fields, buttons, and labels.

To get a scrollable view, we need to move our components into a UIScrollView instead of a UIView.

Time to get back into Interface Builder...



254   Chapter 6

www.it-ebooks.info



saving, editing, and sorting data



Wrap your content in a scroll view

We want the user to be able to scroll through our controls when the keyboard

covers some of them up. In order to do that, we need to add a UIScrollView to

our view and then tell it about the controls (the content view) we want it to handle.



The scroll view

needs to hold

these components

now.



All these

components

need to be

children of

the scroll view.



e

The scroll view will be wth

vie

e

size of the entir l).

(minus the nav contro

This is really annoying. You mean we

have to pull all those components

off and then lay out the view again?

Isn’t there an easier way?



You’ve got a point.

Remember when we said sometimes

Interface Builder makes things (a lot)

easier? This is one of those times...



you are here 4  255

www.it-ebooks.info



scroll view construction



Easy GUI REConstruction

Apparently we aren’t the first people to realize after

we’ve built a view that it needs to be scrollable.

Interface Builder has built-in support for taking an

existing view and wrapping it in a UIScrollView.

Highlight all the widgets (as shown

here) in the detail view, then go to the

Editor→Embed→Scroll View menu option.

Interface Builder will automatically create a new

scrolled view and stick all the widgets in the same

location on the scrolled view.



Now you have the

same listing of

widgets as before,

but they are

under a scroll view.



Interface Builder

will create a

UIScrollView just

big enough to hold

all our components.

Since we want

the whole view

to scroll, grab the

corners of the new

UIScrollView and

drag them out to

the corners of the

screen, right up

to the edge of the

navigation bar (we

don’t want that to

scroll).



How will this new scroll view know how much

content needs to be scrolled?



256   Chapter 6

www.it-ebooks.info



saving, editing, and sorting data



The scroll view is the same size as

the screen

Interface Builder created the UIScrollView, but there are a few

finishing touches necessary to make this work the way we want.

You need to tell the UIScrollView how big its content area is

so it knows what it will need to scroll—you do that by setting

its contentSize property. Then you’ll need to add an outlet and

property for the UIScrollView, then wire it up in Interface Builder

so we can get to it.

So how do we figure out how big the contentSize should be?

When the UIScrollView is the same size as our screen, we don’t

have anything outside of the visible area that it needs to worry

about. Since the scroll view is the same size as our UIView that it’s

sitting in, we can grab the size from there, like this:

scrollView.contentSize = self.view.frame.size;



Once you’ve added that line, you’ll have a scroll view that takes

up all of the available space, and it thinks its content view is the

same size.



Once you re

UIScrollViewsiza e it, the

are the same nd its contentSize

tell that to tsize. We just need to

he scroll view.



Update DrinkDetailViewController.h and DrinkDetailViewController.m to

handle our new UIScrollView.



1



 dd an attribute named scrollView to DrinkDetailViewController to hold a reference

A

to the UIScrollView. You’ll need the field declaration and IBOutlet property, then you

will synthesize it in the .m and release it in dealloc.



2



 ire up the new property to the UIScrollView in Interface Builder by adding a new

W

Referencing Outlet to the UIScrollView connected to your scrollView property.



3



 et the initial contentSize for the scrollView in viewDidLoad:. Remember, we’re

S

telling the scrollView that its content is the same size as the view it’s sitting in.



you are here 4  257

www.it-ebooks.info



start scrolling



Update your DrinkDetailViewController.h and

DrinkDetailViewController.m to handle our new UIScrollView.



 dd an attribute named scrollView to DrinkDetailViewController to hold a reference

A

to the UIScrollView. You’ll need the field declaration, an IBOutlet property, to

synthesize it in the .m and release it in dealloc.



1



@interface DrinkDetailViewController : UIViewController {

@private





UITextField *nameTextField_;







UITextView *directionsTextView_;







UITextView *ingredientsTextView_;







Add a f

the new siecld and a property

rollView.

for



NSDictionary *drink_;



UIScrollView *scrollView_;







}



@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;



ty,

Synthesize the proper

e

tS

en

then set the cont iz

in viewDidLoad.



DrinkDetailViewController.h



Clean up our

reference in dealloc.



@synthesize scrollView=scrollView_;



-



(void)viewDidLoad {



Set the initial

contentSize.



[super viewDidLoad];

self.scrollView.contentSize = self.

view.frame.size;



}



- (void)dealloc {





[scrollView_ release];



[nameTextField_ release];



[ingredientsTextView_

release];



...



DrinkDetailViewController.m



258   Chapter 6

www.it-ebooks.info



saving, editing, and sorting data



2



Wire up the new property to the UIScrollView in Interface Builder.



Test Drive

Tap in the text field

and the keyboard

appears...but nothing’s

scrolling!



Why isn’t it working yet? Think about all the things

that you have going into this view—the scroll view,

the main view, and the keyboard...



you are here 4  259

www.it-ebooks.info



keyboard means changes



The keyboard changes the visible area

The problem is that the keyboard changes the visible area but the scroll

view has no idea that just happened. The scroll view still thinks it has

the whole screen to display its content, and from its perspective, that’s

plenty of room. We need to tell the scroll view that the visible area is

smaller now that the keyboard is there.



Content view



ent

In DrinkMixer, the coasntour

view is the same size e, which

scroll view’s initial siz

is the whole screen...



appears

...but then the keyboaanrdd covers up

over the scroll view visible area.

a large part of the scroll view it

We need to tell the with.

has less space to work



Scroll view



iOS tells you about the keyboard, but

doesn’t tinker with your views.

Just because iOS knows that the keyboard is

there, it doesn’t know how your app wants

to handle it. That’s up to you!



260   Chapter 6

www.it-ebooks.info



saving, editing, and sorting data



Wouldn't it be dreamy if iOS could just tell

the app when the keyboard appears? But I

know it's just a fantasy…



you are here 4  261

www.it-ebooks.info



iOS notifications



iOS notifies you about the keyboard

Interacting with the keyboard and the scroll view brings us to a part

of the iOS we haven’t talked about yet called Notifications. Just

like component events being passed around our application, there are

system-level events, called Notifications, that are being passed by iOS.

The secret to knowing what’s going on with the keyboard is knowing

about these events.

1



 am taps in the Drink

S

name field and the

field becomes the first

responder. Now the

iOS needs to show the

keyboard.



Event



UIKeyboardDidShowNotification



 he iOS posts a notification to the

T

default NSNotificationCenter named

UIKeyboardDidShowNotification.



2



Object



Selector



DetailDrinkViewController



keyboardDidShow



NSNotificationCenter



4



3



 he NSNotificationCenter

T

invokes the target selector

and passes it information

about the object that

triggered the event, along

with event-specific details.

[registeredObject

keyboardDidShow:eventInfo];



262   Chapter 6

www.it-ebooks.info



 SNotificationCenter looks up the

N

event to see if anyone is registered

to be told when that event happens.

Objects are registered by providing a

selector (method) to call if the event

is triggered.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 6. Saving, Editing, and Sorting Data: Everyone’s an editor...

Tải bản đầy đủ ngay(0 tr)

×