ADBoundViewController

open class ADBoundViewController: UIViewController

A ADBoundViewController provides a method to attach it to a data model (any Swift class or structure that conforms to the Codable protocol) and any control conforming to the ADBindable protocol on any View or SubView will automatically be populated with the values from the data model based on the dataPath property of the control. The dataPath must match the name and case of a field on the attached data model or be a formula using a SQL like syntax.

By using the setDataModel function to set the model and automatically populate the fields, you to not need to create Actions or Outlets for the individual controls. The ADBoundViewController will automatically handle the reading and writing of properties for you.

For any ADBindable control that is editable, calling the getDataModel function will return the values from the controls on the View and SubViews. These will be written to the field of the data model as specified by the dataPath property of the control, as a result, a formula should not be used for the dataPath of any field that is readable.

For any field that requires onscreen keyboard support, the ADBoundViewController will automatically handle keyboard events such as moving fields hidden by the keyboard so they are visible and moving them back after they lose focus. Accessory tools can be automatically added to the keyboard to move between the text entry fields and to close the keyboard based on options for each control. These buttons can be customized using the prevButtonText, prevButtonImage, nextButtonText, nextButtonImage, doneButtonText and doneButtonImage properties of the ADBoundViewController.

The ADBoundTextPicker control has an embedded picker view that will be handled automatically by the ADBoundViewController in a similar fashion as th onscreen keyboard. If the field is covered by the picker, it will be moved automatically to be shown and moved back when the picker is closed. An accessory toolbar will be added to the picker that can be customized with the cancelButtonText, cancelButtonImage, doneButtonText and doneButtonImage properties of the ADBoundViewController.

Example:

// Given the following class
class Category: ADDataTable {

    enum CategoryType: String, Codable {
        case local
        case web
    }

    static var tableName = "Categories"
    static var primaryKey = "id"
    static var primaryKeyType: ADDataTableKeyType = .computedInt

    var id = 0
    var added = Date()
    var name = ""
    var description = ""
    var enabled = true
    var highlightColor = UIColor.white.toHex()
    var type: CategoryType = .local
    var icon: Data = UIImage().toData()

    required init() {

    }
}

// Populate any control on the View with values from the data model
let category = Category(name: "Swift File", description: "A file containing swift source code.")
do {
   try setDataModel(category)
} catch {
   print("Failed to set data model")
}

// Read the vaules back from the controls
do {
    let category = try getDataModel(Category.self)
    print("Category Description: \(category.description)")
} catch {
   print("Unable to read data model")
}
  • A ADRecord representing the attached data model as a set of key/value pairs where the key is the name of the field that the value was read from or is writtne to. In between setDataModel and getDataModel function calls, use this property to interact with the values from the data model.

    Declaration

    Swift

    public var record: ADRecord = [:]
  • Sets the text of the Previous button for ADBoundTextField and ADBoundTextView controls that show the previous and next accessory buttons.

    Declaration

    Swift

    @IBInspectable public var prevButtonText: String = "<"
  • Sets the image of the Previous button for ADBoundTextField and ADBoundTextView controls that show the previous and next accessory buttons.

    Declaration

    Swift

    @IBInspectable public var prevButtonImage: UIImage?
  • Sets the text of the Next button for ADBoundTextField and ADBoundTextView controls that show the previous and next accessory buttons.

    Declaration

    Swift

    @IBInspectable public var nextButtonText: String = ">"
  • Sets the image of the Next button for ADBoundTextField and ADBoundTextView controls that show the previous and next accessory buttons.

    Declaration

    Swift

    @IBInspectable public var nextButtonImage: UIImage?
  • Sets the text of the Done button for ADBoundTextField, ADBoundTextPicker and ADBoundTextView controls that show the done accessory button.

    Declaration

    Swift

    @IBInspectable public var doneButtonText: String = "Done"
  • Sets the image of the Done button for ADBoundTextField, ADBoundTextPicker and ADBoundTextView controls that show the done accessory button.

    Declaration

    Swift

    @IBInspectable public var doneButtonImage: UIImage?
  • Sets the text of the Cancel button for ADBoundTextPicker control that show the cancel accessory button.

    Declaration

    Swift

    @IBInspectable public var cancelButtonText: String = "Cancel"
  • Sets the image of the Cancel button for ADBoundTextPicker control that show the cancel accessory button.

    Declaration

    Swift

    @IBInspectable public var cancelButtonImage: UIImage?
  • Initializes a new instance of the ADBoundViewController.

    Declaration

    Swift

    required public init?(coder aDecoder: NSCoder)
  • Attaches the given data model (any Swift class or structure conforming to the Codeable protocol) to the ADBoundViewController populating the record property and any control conforming to the ADBindable protocol on any View or SubView.

    Example:

    // Given the following class
    class Category: ADDataTable {
    
        enum CategoryType: String, Codable {
            case local
            case web
        }
    
        static var tableName = "Categories"
        static var primaryKey = "id"
        static var primaryKeyType: ADDataTableKeyType = .computedInt
    
        var id = 0
        var added = Date()
        var name = ""
        var description = ""
        var enabled = true
        var highlightColor = UIColor.white.toHex()
        var type: CategoryType = .local
        var icon: Data = UIImage().toData()
    
        required init() {
    
        }
    }
    
    // Populate any control on the View with values from the data model
    let category = Category(name: "Swift File", description: "A file containing swift source code.")
    do {
       try setDataModel(category)
    } catch {
       print("Failed to set data model")
    }
    

    Declaration

    Swift

    public func setDataModel<T:Encodable>(_ value: T) throws

    Parameters

    value

    A Swift class or structure conforming to the Codeable protocol.

  • Reads the values from any editable control conforming to the ADBindable protocol into the given data model (any Swift class or structure conforming to the Codeable protocol).

    Example:

    // Given the following class
    class Category: ADDataTable {
    
        enum CategoryType: String, Codable {
            case local
            case web
        }
    
        static var tableName = "Categories"
        static var primaryKey = "id"
        static var primaryKeyType: ADDataTableKeyType = .computedInt
    
        var id = 0
        var added = Date()
        var name = ""
        var description = ""
        var enabled = true
        var highlightColor = UIColor.white.toHex()
        var type: CategoryType = .local
        var icon: Data = UIImage().toData()
    
        required init() {
    
        }
    }
    
    // Read the vaules back from the controls
    do {
       let category = try getDataModel(Category.self)
       print("Category Description: \(category.description)")
    } catch {
       print("Unable to read data model")
    }
    

    Declaration

    Swift

    public func getDataModel<T:Decodable>(_ type: T.Type) throws -> T

    Parameters

    type

    The type of a Swift class or structure conforming to the Codeable protocol to read the values into and return.

    Return Value

    A class of the given type populated with any values read from the control conforming to the ADBindable protocol on any View or SubView controlled by this ADBoundViewController.

  • Forces the ADBoundViewController to update the values of any control conforming to the ADBindable protocol on any View or SubView controlled by this ADBoundViewController with the values from the record property.

    Declaration

    Swift

    public func updateBindings()
  • Forces the ADBoundViewController to update the record property with the values from any editable control conforming to the ADBindable protocol on any View or SubView controlled by this ADBoundViewController.

    Declaration

    Swift

    public func updateRecord()
  • Forces the ADBoundViewController to read the values from any bound control in the controls array into the record property and write the values back to the bound controls to refresh the view.

    Declaration

    Swift

    public func refreshDisplay()
  • Called by a ADBoundTextField or ADBoundTextView when it becomes first responder to see if the field is under the onscreen keyboard. If the field is under the keyboard, the field will be moved to expose the keyboard. If the view has already been moved and the field is not under the keyboard, the view will be moved back to its original position.

    Declaration

    Swift

    public func moveViewToExposeField(withFrame fieldFrame: CGRect)

    Parameters

    fieldFrame

    The GCRect of the text field or view that has just gained focus.

  • Called by a ADBoundTextField or ADBoundTextView to see if another ADBoundTextField or ADBoundTextView is in a higher location on the form.

    Declaration

    Swift

    public func hasPrevTextFieldOrView(beforeField id: Int) -> Bool

    Parameters

    id

    The form ID of the control performing the check.

    Return Value

    true if there is a previous text field or view, else returns false.

  • Called by a ADBoundTextField or ADBoundTextView to see if another ADBoundTextField or ADBoundTextView is in a lower location on the form.

    Declaration

    Swift

    public func hasNextTextFieldOrView(afterField id: Int) -> Bool

    Parameters

    id

    The form ID of the control performing the check.

    Return Value

    true if there is a next text field or view, else returns false.

  • Called by a ADBoundTextField or ADBoundTextView to move to a previous text field or view.

    Declaration

    Swift

    public func moveToPrevTextFieldOrView(beforeField id: Int)

    Parameters

    id

    The form ID of the control performing the move.

  • Called by a ADBoundTextField or ADBoundTextView to move to a next text field or view.

    Declaration

    Swift

    public func moveToNextTextFieldOrView(afterField id: Int)

    Parameters

    id

    The form ID of the control performing the move.

  • Scans the Views and SubViews controlled by this ADBoundViewController for any controls conforming to the ADBindable protocol and monitors any keyboard events.

    Declaration

    Swift

    override open func viewWillAppear(_ animated: Bool)
  • Stops monitoring keyboard events.

    Declaration

    Swift

    override open func viewWillDisappear(_ animated: Bool)