Database Reference
In-Depth Information
If you enter any value above 5 and try to save, the app rejects your rating and asks
you to try again with a new alert view. Voila!
Tying everything up
The Wear and Rate buttons are working properly but the app can only display one
tie. Tapping the different values on the segmented control is supposed to switch
ties. You will finish up this sample project by implementing that feature.
Every time the user taps the segmented control, it executes the
segmentedControl
action method in your code. This method is currently blank; implement it as shown
below:
@IBAction
func
segmentedControl(control:
UISegmentedControl
) {
let
selectedValue =
control.
titleForSegmentAtIndex
(control.
selectedSegmentIndex
)
let
fetchRequest =
NSFetchRequest
(entityName:
"Bowtie"
)
fetchRequest.
predicate
=
NSPredicate
(format:
"searchKey == %@"
, selectedValue!)
var
error:
NSError
?
let
results =
managedContext
.
executeFetchRequest
(fetchRequest,
error: &error)
as
[
Bowtie
]?
if
let
bowties = results {
currentBowtie
= bowties.
last
!
populate
(
currentBowtie
)
}
else
{
println
(
"Could not fetch
\(error)
,
\(error!.
userInfo
)
"
)
}
}
The title of each segment in the segmented control conveniently corresponds to a
particular tie's
searchKey
attribute. Grab the title of the currently selected segment
and fetch the appropriate bow tie using a well-crafted
NSPredicate
.
Then, use the last bow tie in the array of results (there should only be one per
searchKey
) to populate the user interface.
Once again, build and run the app. Tap different letters on the segmented control
for a psychedelic treat: