Overriding Personalization View Controllers

If you don’t like a default personalization view controller or you need to make significant changes beyond basic theming and customization, you can override the entire View Controller for a PersonalizationOption and use your own.

If you are using the VoucherCreationViewController and the VoucherCreationFlowCoordinator to launch the create flow, overriding is done by following these steps:

  1. Create your custom View Controller and make it conform to the PersonalizationOptionViewControllerProtocol. This is a simple protocol that has an initalizer and a PersonalizationOptionViewControllerDelegate.
  2. Call the PersonalizationOptionViewControllerDelegate methods for cancel and finish when you dismiss your custom view controller.
  3. Implement the

    func personalizationViewController(for personalizationOption: PersonalizationOption) -> UIViewController & PersonalizationOptionViewControllerProtocol
    

    method of the VoucherCreationFlowDelegate and return your custom view controller for the proper PersonalizationOption.

  4. Optionally implement

    func creationItemImageView(for personalizationOption: PersonalizationOption, personalizedObject: Any) -> CreationItemImageView
    

    of the VoucherCreationFlowDelegate if you want a custom view dropped into the EnvelopeView.

Example

This is a basic example of overriding the TitlePersonalizationOption.

Step 1 and 2

Create a Custom View Controller, implement PersonalizationOptionViewControllerProtocol and call PersonalizationOptionViewControllerDelegate methods.

// SurpriiseTitleViewController.swift

class SurpriiseTitleViewController: UIViewController, PersonalizationOptionViewControllerProtocol {

    @IBOutlet weak var addTitleTextView: UITextView!
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var doneButton: UIButton!

    var delegate: PersonalizationOptionViewControllerDelegate!
    var titlePersonalizationOption:TitlePersonalizationOption?

    static func viewController(with personalizationOption: PersonalizationOption!, createManager: VoucherCreationManager!, createFlowDelegate: VoucherCreationFlowDelegate!) -> Self? {
        let viewController:SurpriiseTitleViewController = SurpriiseTitleViewController(nibName: NSStringFromClass(self), bundle: Bundle.main);
        viewController.titlePersonalizationOption = personalizationOption as? TitlePersonalizationOption

        return unsafeDowncast(viewController, to: self);
    }

    static func supportsPersonalizationOption(_ personalizationOption: PersonalizationOption!) -> Bool {
        return personalizationOption.type == PersonalizationType.title
    }

    @IBAction func onDone(sender: UIButton) {
        self.dismiss(animated: true) {
            if (self.delegate.responds(to:#selector(PersonalizationOptionViewControllerDelegate.personalizationOptionViewControllerCancelled(_:)))) {
                self.delegate.personalizationOptionViewControllerCancelled(self)
            }
        }
    }

    @IBAction func onCancel(sender: UIButton) {
        self.dismiss(animated: true) {
            if (self.delegate.responds(to: #selector(PersonalizationOptionViewControllerDelegate.personalizationOptionViewController(_:personalizationOption:selectedPersonalizedObject:)))) {
                self.delegate.personalizationOptionViewController(self, personalizationOption: self.titlePersonalizationOption, selectedPersonalizedObject: ["title": self.addTitleTextView.text])
            }
        }
    }
}

Step 3 and 4

Implement VoucherCreationFlowDelegate methods.

func personalizationViewController(for personalizationOption: PersonalizationOption) -> (UIViewController & PersonalizationOptionViewControllerProtocol)? {
    if (personalizationOption.type == PersonalizationType.title) {
        return SurpriiseTitleViewController.viewController(with: personalizationOption, createManager: nil, createFlowDelegate: nil)!
    } else {
        return nil
    }
}

func creationItemImageView(for personalizationOption: PersonalizationOption, personalizedObject: Any) -> CreationItemImageView? {
    if (personalizationOption.type == PersonalizationType.title) {
        let label = UILabel(frame: CGRect.zero)
        label.text = (personalizedObject as! Dictionary)["title"]
        label.backgroundColor = UIColor.red
        label.textColor = UIColor.white
        label.sizeToFit()
        let imageView = CreationItemImageView.init(type: PersonalizationType.title, subType: 0, itemId: 0, isEditable: true, isRemovable: true, viewToRenderAsImage:label, borderWidth: 0, borderColor: nil)
        imageView?.frame.size = label.frame.size
        return imageView
    } else {
        return nil
    }
}