Skip to content

An organizational tool for writing custom view controllers using UIKit.

License

Notifications You must be signed in to change notification settings

ustwo/baseviewcontroller-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License Build Status codecov.io Platform Swift

BaseViewController

The BaseViewController framework provides an organizational tool for writing custom view controllers using UIKit. This framework is written using Swift 3.0. It has been developed and used by iOS developers at ustwo.

Dependencies

Installation

BaseViewControllerSwift version vs Swift version.

Because of the many Swift versions BaseViewControllerSwift supports, it might be confusing to find the version of BaseViewControllerSwift that you need. Below is a table that shows which version of BaseViewControllerSwift you should use for your Swift version.

Swift version BaseViewControllerSwift version
5.0 3.0.0
3.0 2.0.0
2.1 1.0.0

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate BaseViewControllerSwift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.3'
use_frameworks!

pod 'BaseViewControllerSwift', '~> 2.0'

Then, run the following command:

$ pod install

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate BaseViewControllerSwift into your project manually. Add the BaseViewController.swift file from the Sources folder to your Xcode project.

Usage

Use the BaseViewController as a base class for all your custom view controllers. Then override our common setup functions and have them automatically called as part of the initialization.

BaseViewController itself is a subclass of UIViewController. It contains two empty setup functions that are called by viewDidLoad(). These commonly used setup functions are setupView() and setupAccessibility() and are called in that order.

In addition, BaseViewController is also a generic class that takes a UIView as a type parameter. During the loadView() phase, it instantiates the view and sets it as the controller's view. BaseViewController also provides a underlyingView property for accessing the view without needing to type cast.

Examples

Here are some example implementations of using BaseViewController. In all of the examples we call super on the setup function, but do this as appropriate in your own code.

Controller Definition

When defining your custom view controller, inherit from BaseViewController and specify your custom view. There is no need to instantiate your custom view or add an accessing property. This is all done by BaseViewController.

final class ProfileViewController: BaseViewController<ProfileView> {
  ...
}

setupView

Use the setupView() function to add target/actions to controls, set dynamically generated values, etc.

override func setupView() {
	super.setupView()

	underlyingView.titleLabel.text = profile.name
	underlyingView.finishedButton.addTarget(self, action: #selector(ProfileViewController.finishedButtonPressed(_:)), for: .touchUpInside)
}

setupAccessibility

Use the setupAccessibility() function to add dynamically generated accessibility information to the view.

override func setupAccessibility() {
	super.setupAccessibility()

	underlyingView.profileImage.accessibilityLabel = NSLocalizedString("Image of", comment: "") + " " +  profile.name
}

Maintainers

  • Shagun Madhikarmi (@madhikarma)
  • Aaron McTavish (@aamctustwo)

Contact

License

BaseViewControllerSwift is released under the MIT License. See the LICENSE file.