1. Trang chủ
  2. » Công Nghệ Thông Tin

Ebook IOS programming The big nerd ranch guide (5th edition) Part 2

371 767 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 371
Dung lượng 13,26 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

(BQ) Part 2 book IOS programming The big nerd ranch guide has contents Subclassing UITable view cell, uinavigationcontroller, stack views, touch events and uiresponder, uigesturerecognizer and uimenucontroller, web services, collection views, core data relationships,...and other contents.

Trang 1

11 Subclassing UITableViewCell

Item’s name, its value in dollars, and its serial number (Figure 11.1)

Figure 11.1 Homepwner with subclassed table view cells

You customize the appearance of UITableViewCell subclasses by adding subviews to

its contentView Adding subviews to the contentView instead of directly to the cellitself is important because the cell will resize its contentView at certain times Forexample, when a table view enters editing mode, the contentView resizes itself tomake room for the editing controls (Figure 11.2) If you added subviews directly to the

UITableViewCell, these editing controls would obscure the subviews The cell cannotadjust its size when entering edit mode (it must remain the width of the table view), butthe contentView can resize, and it does

Trang 2

Figure 11.2 Table view cell layout in standard and editing mode

Trang 3

Create a new Swift file named ItemCell In ItemCell.swift, define ItemCell as

a UITableViewCell subclass.

i​ p​r​ ​o​ n​a​ i​n​

i​ p​r​ ​I​ i​

c​ a​s​ I​e​ C​l​ :​U​ T​b​ e​i​ w​e​ l​{​

Trang 4

5 Select the bottom-left label and open its size inspector Find the Vertical Content Hugging Priority and lower it to 250 Lower the Vertical Content Compression Resistance Priority to 749 You will learn what these Auto Layout properties do in Chapter 12.

6 Your frames might be misplaced, so open the Resolve Auto Layout Issues menu andupdate the frames for the three labels

Trang 6

For ItemsViewController to configure the content of an ItemCell in

tableView(_:cellForRowAtIndexPath:), the cell must have properties thatexpose the three labels These properties will be set through outlet connections in

Main.storyboard

The next step, then, is to create and connect outlets on ItemCell for each of its

subviews

Open ItemCell.swift and add three properties for the outlets

i​ p​r​ ​I​ i​

c​ a​s​ I​e​ C​l​ :​U​ T​b​ e​i​ w​e​ l​{​

​ ​@​ B​u​ l​t​ v​r​ n​m​ L​b​ l​ ​ I​a​ e​!​

​ ​@​ B​u​ l​t​ v​r​ s​r​ a​N​ m​e​ L​b​ l​ ​ I​a​ e​!​

​ ​@​ B​u​ l​t​ v​r​ v​l​ e​a​ e​:​ U​L​ b​l​

}​

You are going to connect the outlets for the three views to the ItemCell When

connecting outlets earlier in the book, you Control-dragged from view controller in the

storyboard to the appropriate view But the outlets for ItemCell are not outlets on a controller They are outlets on a view: the custom UITableViewCell subclass.

Trang 8

height of the table view cells

o​ e​r​ d​ ​ u​c​ v​e​ D​d​ o​d​ )​{​

​ ​s​ p​r​ v​e​ D​d​ o​d​ )​

​ ​/​ ​e​ ​h​ ​e​ g​t​ o​ ​ h​ ​ t​t​ s​b​ r​

​ ​l​ t​s​ a​u​ B​r​ e​g​ t​=​ U​A​ p​i​ a​i​ n​s​ a​e​ A​p​ i​ a​i​ n​)​ s​a​ u​B​ r​r​ m​.​ e​g​ t​

​ ​l​ t​i​ s​t​ ​ ​ I​d​ e​n​ e​s​ t​p​ ​t​ t​s​ a​H​ i​h​ ,​ l​f​ :​0​ ​o​ t​m​ ​,​ r​g​ t​ ​ )​

​ ​t​ b​e​ i​w​ c​n​ e​t​ n​e​ ​ ​ n​e​ s​

​ ​t​ b​e​ i​w​ s​r​ l​I​ d​c​ t​r​ n​e​ s​=​ i​s​ t​

​ ​t​ b​e​ i​w​ r​w​ e​g​ t​=​ 6​

o​ e​r​ d​ ​ u​c​ t​b​ e​i​ w​t​ b​e​ i​w​ ​I​ a​l​ V​e​ ,​

​ ​ ​ ​c​ l​F​ r​o​ A​I​ d​x​ a​h​ i​d​ x​a​ h​ ​ S​n​ e​P​ t​ )​-​ ​I​ a​l​ V​e​ C​l​ ​

​ ​/​ ​e​ ​ ​ e​ ​ r​r​ c​c​ e​ ​ e​l​

​ ​l​ t​c​ l​ ​ ​a​ l​V​ e​.​ e​u​ u​R​ u​a​ l​C​ l​W​ t​I​ e​ t​f​ e​(​ U​T​ b​e​ i​w​ e​l​ ,​

​ ​ ​ ​f​ r​n​ e​P​ t​:​ i​d​ x​a​ h​

​ ​l​ t​c​ l​ ​ ​a​ l​V​ e​.​ e​u​ u​R​ u​a​ l​C​ l​W​ t​I​ e​ t​f​ e​(​ I​e​ C​l​ "​

​ ​ ​ ​f​ r​n​ e​P​ t​:​ i​d​ x​a​ h​ ​ s​ ​ t​m​ e​l​

​ ​/​ ​e​ ​h​ ​e​ t​o​ ​h​ ​e​ l​w​ t​ ​ h​ ​ e​c​ i​t​ o​ ​f​ t​e​ i​e​

​ ​/​ ​h​ t​i​ ​t​ t​e​ n​h​ i​d​ x​o​ ​t​ m​,​ w​e​ e​n​ =​ r​w​ t​i​ ​e​ l​

​ ​/​ ​i​ l​a​ p​a​ ​n​ o​ ​ h​ ​ a​l​ v​e​

​ ​l​ t​i​ e​ ​ ​t​ m​t​ r​.​ l​I​ e​s​ i​d​ x​a​ h​r​ w​

​ ​c​ l​.​ e​t​ a​e​ ?​t​ x​ ​ ​t​ m​n​ m​

​ ​c​ l​.​ e​a​ l​e​ t​a​ e​?​ t​x​ ​ ​ $​(​ t​m​ v​l​ e​n​ o​ l​r​ )​

​ ​/​ ​o​ f​g​ r​ ​ h​ ​ e​l​ w​t​ ​h​ ​t​ m​

​ ​c​ l​.​ a​e​ a​e​ ​e​ t​=​ i​e​ ​a​ e​

​ ​c​ l​.​ e​i​ l​u​ b​r​ a​e​ ​e​ t​=​ i​e​ ​e​ i​l​ u​b​ r​

​ ​c​ l​.​ a​u​ L​b​ l​t​ x​ ​ ​$​ (​t​ m​v​ l​e​ n​o​ l​r​ )​

​ ​r​ t​r​ ​e​ l​

}​

First, the reuse identifier is updated to reflect your new subclass The code at the end ofthis method is fairly obvious – for each label on the cell, set its text to some property

from the appropriate Item.

Trang 9

values from each Item.

Trang 11

Currently, the cells have a fixed height of 65 points It is much better to allow the content

of the cell to drive its height That way, if the content ever changes, the table view cell’sheight can change automatically

You can achieve this goal, as you have probably guessed, with Auto Layout The

UITableViewCell needs to have vertical constraints that will exactly determine the

height of the cell Currently, ItemCell does not have sufficient constraints for this You

need to add a constraint between the two left labels that fixes the vertical spacing betweenthem

​ ​/​ ​e​ ​h​ ​e​ g​t​ o​ ​ h​ ​ t​t​ s​b​ r​

​ ​l​ t​s​ a​u​ B​r​ e​g​ t​=​ U​A​ p​i​ a​i​ n​s​ a​e​ A​p​ i​ a​i​ n​)​ s​a​ u​B​ r​r​ m​.​ e​g​ t​

​ ​l​ t​i​ s​t​ ​ ​ I​d​ e​n​ e​s​ t​p​ ​t​ t​s​ a​H​ i​h​ ,​ l​f​ :​0​ ​o​ t​m​ ​,​ r​g​ t​ ​ )​

​ ​t​ b​e​ i​w​ c​n​ e​t​ n​e​ ​ ​ n​e​ s​

​ ​t​ b​e​ i​w​ s​r​ l​I​ d​c​ t​r​ n​e​ s​=​ i​s​ t​

​ ​t​ b​e​ i​w​ r​w​ e​g​ t​=​ 6​

​ ​t​ b​e​ i​w​ r​w​ e​g​ t​=​ U​T​ b​e​ i​w​ u​o​ a​i​ D​m​ n​ i​n​

​ ​t​ b​e​ i​w​ e​t​ m​t​ d​o​ H​i​ h​ ​ ​5​

Build and run the application The application will look the same as it did before In the

next section, you will learn about a technology called Dynamic Type that will take

advantage of the automatically resizing table view cells

Trang 13

Creating an interface that appeals to everyone can be daunting Some people prefer morecompact interfaces so they can see more information at a time Others might want to beable to easily see information at a glance, or perhaps they have poor eyesight In short:people have different needs Good developers strive to make apps that meet those needs.Dynamic Type is a technology that helps realize this goal by providing specifically

different text styles

Trang 14

Open Main.storyboard Let’s update the labels to use the text styles instead of fixedfonts Select the nameLabel and valueLabel and open the attributes inspector Click on the texticon to the right of Font For Font, choose Text Styles - Body (Figure 11.8) Repeat the samesteps for the serialNumberLabel, choosing the Caption 1 text style

Figure 11.8 Changing the text style

Now let’s change the preferred font size You do this through the Settings application

Build and run the application Press the Home button (or use Home from the Hardware

Trang 15

Larger Text (On an actual device, this menu is accessed in Settings under Display & Brightness

and then Text Size.) Drag the slider all the way to the left to set the font size to the smallestvalue (Figure 11.9) Press the Home button again to save these changes

Figure 11.9 Text size settings

Build and run the application (If you switch back to the application, either using the taskswitcher or through the Home screen, you will not see the changes You will fix that in thenext section.) Add some items to the table view and you will see the new smaller fontsizes in action

Responding to user changes

When the user changes the preferred text size and returns to the application, the table viewwill get reloaded Unfortunately, the labels will not know about the new preferred text

Trang 16

Open ItemCell.swift and add a new method that updates all three labels

f​ n​ ​ p​a​ e​a​ e​s​ )​{​

​ ​l​ t​b​ d​F​ n​ ​ ​I​ o​t​ p​e​ e​r​ d​o​ t​o​ T​x​ S​y​ e​ U​F​ n​T​ x​S​ y​e​ o​y​

​ ​n​ m​L​ b​l​ f​n​ ​ ​ o​y​ o​t​

​ ​v​ l​e​ a​e​ ​o​ t​=​ b​d​ F​n​

​ ​l​ t​c​ p​i​ n​F​ n​ ​ ​I​ o​t​ p​e​ e​r​ d​o​ t​o​ T​x​ S​ y​e​ U​F​ n​T​ x​S​ y​e​ a​t​ o​1​

​ ​s​ r​a​ N​m​ e​L​ b​l​ f​n​ ​ ​ a​t​ o​1​ o​t​

}​

Now open ItemsViewController.swift and call this method in

o​ e​r​ d​ ​ u​c​ t​b​ e​i​ w​t​ b​e​ i​w​ ​I​ a​l​ V​e​ ,​

​ ​c​ l​F​ r​o​ A​I​ d​x​ a​h​ i​d​ x​a​ h​ ​ S​n​ e​P​ t​)​ -​ ​I​ a​l​ V​e​ C​l​ ​

​ ​ ​ ​/​ ​e​ ​ ​ e​ ​ r​r​ c​c​ e​ ​ e​l​

​ ​ ​ ​l​ t​c​ l​ ​ ​a​ l​V​ e​.​ e​u​ u​R​ u​a​ l​C​ l​W​ t​ I​e​ t​f​ e​(​ U​T​ b​e​ i​w​ e​l​ ,​

​ ​ ​ ​ ​ ​f​ r​n​ e​P​ t​:​ i​d​ x​a​ h​ ​ s​ ​ t​m​ e​l​

​ ​ ​ ​/​ ​p​ a​e​ t​e​ l​b​ l​ ​ o​ ​ h​ ​ e​ ​ r​f​ r​e​ ​ e​t​ s​z​

​ ​ ​ ​c​ l​.​ p​a​ e​a​ e​s​ )​

​ ​ ​ ​l​ t​i​ e​ ​ ​t​ m​t​ r​.​ l​I​ e​s​ i​d​ x​a​ h​r​ w​

​ ​ ​ ​c​ l​.​ a​e​ a​e​ ​e​ t​=​ i​e​ ​a​ e​

​ ​ ​ ​c​ l​.​ e​i​ l​u​ b​r​ a​e​ ​e​ t​=​ i​e​ ​e​ i​l​ u​ b​r​

​ ​ ​ ​c​ l​.​ a​u​ L​b​ l​t​ x​ ​ ​$​ (​t​ m​v​ l​e​ n​o​ l​ r​)​

​ ​ ​ ​r​ t​r​ ​e​ l​

}​

Build and run the application Go into Settings and change the preferred reading size to thelargest size Unlike before, you can now switch back to Homepwner, either by opening thetask switcher or through the Home screen, and the table view will update to reflect thenew preferred text size

Trang 18

Update the ItemCell to display the valueInDollars in green if the value is less

than 50 and red if the value is greater than or equal to 50

Trang 20

12 Stack Views

You have been using Auto Layout throughout this book to create flexible interfaces thatscale across device types and sizes Auto Layout is a very powerful technology, but withthat power comes complexity Laying out an interface well often needs a lot of constraints,and it can be difficult to create dynamic interfaces due to the need to constantly add andremove constraints

Often, an interface (or a subsection of the interface) can be laid out in a linear fashion.Let’s think about some of the other applications you have written in this book The Quiz

application that you wrote in Chapter 1 consisted of four subviews that were laid out

vertically The same is true for the WorldTrotter application that you wrote: the

ConversionViewController had a vertical interface consisting of a text field and afew labels

Interfaces that have a linear layout are great candidates for using a stack view A stack

view is an instance of UIStackView that allows you to create a vertical or horizontal

layout that is easy to lay out and manages most of the constraints that you would typicallyhave to manage yourself Perhaps best of all, you are able to nest stack views within otherstack views, which allows you to create truly amazing interfaces in a fraction of the time

In this chapter, you are going to continue working on Homepwner to create an interface for

displaying the details of a specific Item The interface that you create will consist of

multiple nested stack views, both vertical and horizontal (Figure 12.1)

Trang 21

Figure 12.1 Homepwner with stack views

Trang 22

Open your Homepwner project and then open Main.storyboard Drag a new View

Controller from the object library onto the canvas Drag a Vertical Stack View from the objectlibrary onto the view for the View Controller Add constraints to the stack view to pin it to theleading and trailing margins, and pin the top and bottom edges to be 8 points from the topand bottom layout guides

Now drag four instances of UILabel from the object library onto the stack view From

top to bottom, give these labels the text “Name,” “Serial,” “Value,” and “Date Created.”(Figure 12.3)

Trang 23

You can see a problem right away: the labels all have a red border (indicating an AutoLayout problem) and there is a warning that some views are vertically ambiguous Thereare two ways you can fix this issue: by using Auto Layout, or by using a property on thestack view Let’s work through the Auto Layout solution first because it highlights animportant aspect of Auto Layout

Implicit constraints

You learned in Chapter 3 that every view has an intrinsic content size You also learnedthat if you do not specify constraints that explicitly determine the width or height, theview will derive its width or height from its intrinsic content size How does this work?

It does this using implicit constraints derived from a view’s content hugging priorities and its content compression resistance priorities A view has one of these priorities for each

axis:

horizontal content hugging priority

Trang 24

to one another with constraints both between the two views and between each view and itssuperview, as shown in Figure 12.4

Figure 12.4 Two labels side by side

This works great until the superview becomes wider At that point, which label shouldbecome wider? The first label, the second label, or both? As Figure 12.5 shows, the

interface is currently ambiguous

Figure 12.5 Ambiguous layout

This is where the content hugging priority becomes relevant The view with the highercontent hugging priority is the one that does not stretch You can think about the priorityvalue as the “strength” of the rubber band The higher the priority value, the stronger the

Trang 25

Content compression resistance priorities

The content compression resistance priorities determine how much a view resists gettingsmaller than its intrinsic content size Consider the same two labels from Figure 12.4.What would happen if the superview’s width decreased? One of the labels would need totruncate its text (Figure 12.6) But which one?

Figure 12.6 Compressed ambiguous layout

The view with the greater content compression resistance priority is the one that will resistcompression and, therefore, not truncate its text

Select the stack view, either on the canvas or using the document outline Open its

attributes inspector and find the section at the top labeled Stack View One of the propertiesthat determines how the content is laid out is the Distribution property Currently it is set to

Fill, which lets the views lay out their content based on their intrinsic content size Changethe value to Fill Equally This will resize the labels so that they all have the same height,ignoring the intrinsic content size (Figure 12.7) Be sure to read the documentation for theother distribution values that a stack view can have

Trang 26

Select the Name label on the canvas Click the left-most icon in the Auto Layout

constraints menu: This will embed the selected view in a stack view

Select the new stack view and open its attributes inspector The stack view is currently avertical stack view, but you want it to be a horizontal stack view Change the Axis to

Horizontal

Now drag a Text Field from the object library to the right of the Name label Since labels, by

Trang 27

intrinsic content width and the text field stretches The label and the text field currentlyhave the same content compression resistance priorities, which would result in an

ambiguous layout if the text field’s text was too long Open the size inspector for the textfield and set its Horizontal Content Compression Resistance Priority to 749

Stack view spacing

The label and text field look a little squished since there is no spacing between them.Stack views allow you to customize the spacing between items

Select the horizontal stack view and open its attributes inspector Change the Spacing to be

8 points Notice that the text field shrinks to accommodate the spacing, because it is lessresistant to compression than the label

Repeat these same steps for the Serial and Value labels

1 Select the label and click the icon

2 Change the stack view to be a horizontal stack view

3 Drag a text field onto the horizontal stack view and change its horizontal contentcompression resistance priority to be 749

4 Update the stack view to have a spacing of 8 points

There are a couple of other tweaks you will want to make to the interface The verticalstack view needs some spacing The Date Created label should have a center text alignment.And the Name, Serial, and Value labels should be the same width

Select the vertical stack, open its attributes inspector, and update the Spacing to be 8 points.Then select the Date Created label, open its attributes inspector, and change the Alignment to

be centered That solves the first two issues

Although stack views substantially reduce the number of constraints that you need to add

to your interface, some constraints are still important With the interface as is, the textfields do not align on their leading edge due to the difference in the widths of the labels.(The difference is not very noticeable in English, but it becomes more pronounced whenlocalized into other languages.) To solve this, you will add leading edge constraints

between the three text fields

Control-drag from the Name text field to the Serial text field and select Leading Then do thesame for the Serial text field and the Value text field The completed interface will look like

Figure 12.8

Trang 28

Stack views allow you to create very rich interfaces in a fraction of the time it would take

to configure them manually using constraints Constraints are still added, but they arebeing managed by the stack view itself instead of by you Stack views allow you to havevery dynamic interfaces at runtime You can add and remove views from stack views by

using addArrangedSubview(_:), insertArrangedSubview(_:atIndex:), and removeArrangedSubview(_:) You can also toggle the hidden property on a

view in a stack view The stack view will automatically lay out its content to reflect thatvalue

Trang 30

In Main.storyboard, select the ItemCell prototype cell on the Items View Controller.Control-drag from the cell to the new view controller that you set up in the previous

section (Make sure you are Control-dragging from the cell and not the table view!) Ablack panel will appear that lists the possible styles for this segue Select Show from the

Selection Segue section (Figure 12.9)

Figure 12.9 Setting up a show segue

Notice the arrow that goes from the table view controller to the new view controller This

is a segue The icon in the circle tells you that this segue is a show segue – each segue has

Trang 31

Build and run the application Tap a cell and the new view controller will slide up from thebottom of the screen (Sliding up from the bottom is the default behavior when presenting

a view controller modally This can be customized, however, as you saw in Chapter 10

when you presented the UIAlertController modally.)

So far, so good! But there are two problems at the moment: the view controller is not

displaying the information for the Item that was selected, and there is no way to dismiss the view controller to return to the ItemsViewController You will fix the first issue

in the next section, and you will fix the second issue in Chapter 13

Trang 33

i​ p​r​ ​o​ n​a​ i​n​

i​ p​r​ ​I​ i​

c​ a​s​ D​t​ i​V​ e​C​ n​r​ l​e​ :​U​ V​e​ C​n​ r​l​ e​ ​

Your window has become a little cluttered Let’s make some temporary space Hide thenavigator area by clicking the left button in the View control at the top of the workspace(the shortcut for this is Command-0) Then, hide the document outline in Interface Builder byclicking the toggle button in the lower left corner of the editor Your workspace shouldnow look like Figure 12.10

Trang 35

Let go and a pop-up window will appear Enter nameField into the Name field, select

Strong from the Storage pop-up menu, and click Connect (Figure 12.13)

Trang 36

After making the connections, DetailViewController.swift should look likethis:

i​ p​r​ ​I​ i​

c​ a​s​ D​t​ i​V​ e​C​ n​r​ l​e​ :​U​ V​e​ C​n​ r​l​ e​ ​

​ ​@​ B​u​ l​t​ v​r​ n​m​ F​e​ d​ ​ I​e​ t​i​ l​!​

​ ​@​ B​u​ l​t​ v​r​ s​r​ a​N​ m​e​ F​e​ d​ ​ I​e​ t​i​ l​!​

​ ​@​ B​u​ l​t​ v​r​ v​l​ e​i​ l​:​ U​T​ x​F​ e​d​

​ ​@​ B​u​ l​t​ v​r​ d​t​ L​b​ l​ ​ I​a​ e​!​

}​

If your file looks different, then your outlets are not connected correctly Fix any

disparities between your file and the code shown above in three steps: First, go throughthe Control-drag process and make connections again until you have the four lines shownabove in your DetailViewController.swift Second, remove any wrong code(like non-property method declarations or properties) that got created Finally, check forany bad connections in the storyboard file In Main.storyboard, Control-click on the

Detail View Controller If there are yellow warning signs next to any connection, click the x

icon next to those connections to disconnect them

It is important to ensure that there are no bad connections in an interface file A bad

connection typically happens when you change the name of a property but do not updatethe connection in the interface file Or, you completely remove a property but do notremove it from the interface file Either way, a bad connection will cause your application

to crash when the interface file is loaded

Trang 37

DetailViewController will hold on to a reference to the Item that is being

displayed When its view is loaded, you will set the text on each text field to the

appropriate value from the Item instance.

In DetailViewController.swift, add a property for an Item instance and override viewWillAppear(_:) to set up the interface.

c​ a​s​ D​t​ i​V​ e​C​ n​r​ l​e​ :​U​ V​e​ C​n​ r​l​ e​ ​

​ ​@​ B​u​ l​t​ v​r​ n​m​ F​e​ d​ ​ I​e​ t​i​ l​!​

​ ​@​ B​u​ l​t​ v​r​ s​r​ a​N​ m​e​ F​e​ d​ ​ I​e​ t​i​ l​!​

​ ​@​ B​u​ l​t​ v​r​ v​l​ e​i​ l​:​ U​T​ x​F​ e​d​

​ ​@​ B​u​ l​t​ v​r​ d​t​ L​b​ l​ ​ I​a​ e​!​

​ ​v​ r​i​ e​:​ I​e​ !​

​ ​o​ e​r​ d​ ​ u​c​ v​e​ W​l​ A​p​ a​(​ n​m​ t​d​ ​o​ l​ ​

​ ​ ​ ​s​ p​r​ v​e​ W​l​ A​p​ a​(​ n​m​ t​d​

​ ​ ​ ​n​ m​F​ e​d​ t​x​ ​ ​ t​m​ n​m​

​ ​ ​ ​s​ r​a​ N​m​ e​F​ e​d​ t​x​ ​ ​ t​m​ s​r​ a​N​ m​e​

​ ​ ​ ​v​ l​e​ i​l​ ​e​ t​=​ "​(​ t​m​ v​l​ e​n​ o​l​ r​)​

​ ​ ​ ​d​ t​L​ b​l​ t​x​ ​ ​ \​i​ e​.​ a​e​ r​a​ e​)​

v​ r​i​ e​:​ I​e​ !​

l​ t​n​ m​e​ F​r​ a​t​ r​ ​ S​u​ b​r​ o​m​ t​e​ ​ ​

​ ​l​ t​f​ r​a​ t​r​ =​N​ N​m​ e​F​ r​a​ t​r​ )​

​ ​f​ r​a​ t​r​ n​m​ e​S​ y​e​ =​.​ e​i​ a​S​ y​e​

​ ​f​ r​a​ t​r​ m​n​ m​m​ r​c​ i​n​ i​i​ s​=​ 2​

​ ​f​ r​a​ t​r​ m​x​ m​m​ r​c​ i​n​ i​i​ s​=​ 2​

​ ​r​ t​r​ ​o​ m​t​ e​

​ ​}​ )​

l​ t​d​ t​F​ r​a​ t​r​ ​S​ a​e​ o​m​ t​e​ ​ ​

​ ​l​ t​f​ r​a​ t​r​ =​N​ D​t​ F​r​ a​t​ r​)​

​ ​f​ r​a​ t​r​ d​t​ S​y​ e​=​ ​e​ i​m​ t​l​

​ ​f​ r​a​ t​r​ t​m​ S​y​ e​=​ ​o​ t​l​

​ ​r​ t​r​ ​o​ m​t​ e​

​ ​}​ )​

o​ e​r​ d​ ​ u​c​ v​e​ W​l​ A​p​ a​(​ n​m​ t​d​ ​o​ l​ ​

​ ​s​ p​r​ v​e​ W​l​ A​p​ a​(​ n​m​ t​d​

​ ​n​ m​F​ e​d​ t​x​ ​ ​ t​m​ n​m​

​ ​s​ r​a​ N​m​ e​F​ e​d​ t​x​ ​ ​ t​m​ s​r​ a​N​ m​e​

​ ​v​ l​e​ i​l​ ​e​ t​=​ "​(​ t​m​ v​l​ e​n​ o​l​ r​)​

​ ​d​ t​L​ b​l​ t​x​ ​ ​ \​i​ e​.​ a​e​ r​a​ e​)​

​ ​v​ l​e​ i​l​ ​e​ t​=​ n​m​ e​F​ r​a​ t​r​ s​r​ n​F​ o​N​ m​ e​(​ t​m​ v​l​ e​n​ o​l​ r​)​

​ ​d​ t​L​ b​l​ t​x​ ​ ​ a​e​ o​m​ t​e​ ​t​ i​g​ r​m​ a​e​ i​ e​.​ a​e​ r​a​ e​)​

Trang 39

between the two view controllers and open the attributes inspector For the identifier, enterShowItem (Figure 12.15)

Figure 12.15 Segue identifier

With your segue identified, you can now pass your Item instances around Open

ItemsViewController.swift and implement

o​ e​r​ d​ ​ u​c​ p​e​ a​e​ o​S​ g​e​ s​g​ e​ ​ I​t​ r​b​ a​d​ e​ u​,​ s​n​ e​:​ A​y​ b​e​ t​)​ {​

​ ​/​ ​f​ t​e​ t​i​ g​r​ d​s​ g​e​ i​ ​ h​ ​ S​o​ I​e​ "​s​ g​ e​

​ ​i​ ​e​ u​.​ d​n​ i​i​ r​=​ ​S​ o​I​ e​"​ {​

​ ​ ​ ​/​ ​i​ u​e​ o​t​ w​i​ h​r​ w​w​ s​j​ s​ ​ a​p​ d​

​ ​ ​ ​i​ ​e​ ​o​ ​ ​ a​l​ V​e​ ​n​ e​P​ t​F​ r​e​ e​t​ d​ o​?​ r​w​ {​

​ ​ ​ ​ ​ ​/​ ​e​ ​h​ ​t​ m​a​ s​c​ a​e​ ​i​ h​t​ i​ ​ o​ ​n​ ​a​ s​i​ ​l​ n​

​ ​ ​ ​ ​ ​l​ t​i​ e​ ​ ​t​ m​t​ r​.​ l​I​ e​s​ r​w​

​ ​ ​ ​ ​ ​l​ t​d​ t​i​ V​e​ C​n​ r​l​ e​

​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ s​g​ e​d​ s​i​ a​i​ n​i​ w​o​ t​o​ l​ r​a​ !​D​ t​i​ V​e​ C​n​ r​l​ e​

​ ​ ​ ​ ​ ​d​ t​i​ V​e​ C​n​ r​l​ e​.​ t​m​ =​i​ e​

​ ​ ​ ​}​

​ ​}​

}​

Build and run the application Tap on a row and the DetailViewController will

Ngày đăng: 16/05/2017, 10:18

TỪ KHÓA LIÊN QUAN