Database Reference
In-Depth Information
This delegate method is similar to didChangeObject ... but notifies you of changes to
sections rather than to individual objects. Here, you handle the cases where
changes in the underlying data trigger the creation or deletion of an entire section.
Take a moment and think about what kind of change would trigger these
notifications. Maybe if a new team entered the World Cup from a completely new
qualifying zone, the fetched results controller would pick up on the uniqueness of
this value and notify its delegate about the new section.
This would never happen in a standard-issue World Cup. Once the 32 qualifying
teams are in the system, there's no way to add a new team. Or is there?
Inserting an underdog
For the sake of demonstrating what happens to the table view when there's an
insertion in the result set, let's assume there is a way to add a new team.
If you were paying close attention, you may have noticed the “+” bar button item
on the top-right. It's been disabled all this time. Perhaps the World Cup has a
secret backdoor entrance!
Let's implement this now. Add the following method to the class in
ViewController.swift :
override func motionEnded(motion: UIEventSubtype ,
withEvent event: UIEvent ) {
if motion == . MotionShake {
addButton . enabled = true
}
}
You override motionEnded so that shaking the device enables the “+” bar button
item. This will be your secret way in. The addButton property held a reference to
this bar button item all along!
Next, add the following method below motionEnded :
@IBAction func addTeam(sender: AnyObject ) {
var alert = UIAlertController (title: "Secret Team" ,
message: "Add a new team" ,
preferredStyle: UIAlertControllerStyle . Alert )
alert. addTextFieldWithConfigurationHandler {
(textField: UITextField !) in
textField. placeholder = "Team Name"
}
 
Search WWH ::




Custom Search