ManagingContacts/ManagingContacts/UpdateGroupViewController.swift

/*
    Copyright (C) 2017 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    A table view controller that allows you to update the name of an 
               existing group.
*/
 
import UIKit
import Contacts
 
class UpdateGroupViewController: UITableViewController {
    // MARK: - Properties
    
    /// Name of the group to be updated.
    var name: String?
    
    /// Textfield used to enter a group name.
    @IBOutlet weak fileprivate var textField: UITextField!
    
    
    // MARK: - View Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Show the name of the group.
        if let displayedName = name {
            textField.text = displayedName
        }
    }
    
    
    // MARK: - UITextFieldDelegate
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        
        // Show the Save button if the user has entered a name.
        navigationItem.rightBarButtonItem!.isEnabled = !(textField.text!.isEmpty)
        return true
    }
    
    
    // MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 && indexPath.row == 0 {
            textField.becomeFirstResponder()
        }
    }
    
    
    // MARK: - Navigation
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Send back the entered/updated name for the group.
        if navigationItem.rightBarButtonItem == sender as? UIBarButtonItem {
            name = textField.text
        }
    }
    
    
    // MARK: - Memory Management
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}