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:
- Create your custom View Controller and make it conform to the
PersonalizationOptionViewControllerProtocol
. This is a simple protocol that has an initalizer and aPersonalizationOptionViewControllerDelegate
. - Call the
PersonalizationOptionViewControllerDelegate
methods for cancel and finish when you dismiss your custom view controller. Implement the
- (UIViewController<PersonalizationOptionViewControllerProtocol> *)personalizationViewControllerForPersonalizationOption:(PersonalizationOption *)personalizationOption
method of the
VoucherCreationFlowDelegate
and return your custom view controller for the properPersonalizationOption
.Optionally implement
- (CreationItemImageView *)creationItemImageViewForPersonalizationOption:(PersonalizationOption *)personalizationOption personalizedObject:(id)personalizedObject
of the
VoucherCreationFlowDelegate
if you want a custom view dropped into theEnvelopeView
.
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.
#import "SurpriiseTitleViewController.h"
@interface SurpriiseTitleViewController ()
@property (nonatomic, weak) IBOutlet UITextView *addTitleTextView;
@property (nonatomic, weak) IBOutlet UIButton *cancelButton;
@property (nonatomic, weak) IBOutlet UIButton *doneButton;
@property (nonatomic) TitlePersonalizationOption *titlePersonalizationOption;
@end
@implementation SurpriiseTitleViewController
#pragma mark - PersonalizationOptionViewControllerProtocol
@synthesize delegate;
+ (instancetype)viewControllerWithPersonalizationOption:(PersonalizationOption *)personalizationOption
createManager:(VoucherCreationManager *)createManager
createFlowDelegate:(id<VoucherCreationFlowDelegate>)createFlowDelegate {
SurpriiseTitleViewController *surpriiseTitleViewController = [[SurpriiseTitleViewController alloc] initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]];
surpriiseTitleViewController.titlePersonalizationOption = (TitlePersonalizationOption *)personalizationOption;
return surpriiseTitleViewController;
}
#pragma mark - IBActions
- (IBAction)onDone:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(personalizationOptionViewController:personalizationOption:selectedPersonalizedObject:)]) {
[self.delegate personalizationOptionViewController:self personalizationOption:self.titlePersonalizationOption selectedPersonalizedObject:@{@"title": self.addTitleTextView.text}];
}
}];
}
- (IBAction)onCancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(personalizationOptionViewControllerCancelled:)]) {
[self.delegate personalizationOptionViewControllerCancelled:self];
}
}];
}
@end
Step 3 and 4
Implement VoucherCreationFlowDelegate
methods.
- (UIViewController<PersonalizationOptionViewControllerProtocol> *)personalizationViewControllerForPersonalizationOption:(PersonalizationOption *)personalizationOption {
if (personalizationOption.type == PersonalizationTypeTitle) {
return [SurpriiseTitleViewController viewControllerWithPersonalizationOption:personalizationOption createManager:nil createFlowDelegate:nil];
} else {
return nil;
}
}
- (CreationItemImageView *)creationItemImageViewForPersonalizationOption:(PersonalizationOption *)personalizationOption personalizedObject:(id)personalizedObject {
if (personalizationOption.type == PersonalizationTypeTitle) {
// turn a label into a `CreationItemImageView`
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = personalizedObject[@"title"];
label.backgroundColor = [UIColor redColor];
label.textColor = [UIColor whiteColor];
[label sizeToFit];
CreationItemImageView *imageView = [CreationItemImageView creationItemImageViewWithType:PersonalizationTypeTitle
subType:0
itemId:0
isEditable:YES
isRemovable:YES
viewToRenderAsImage:label
borderWidth:0
borderColor:nil];
CGRect frame = imageView.frame;
frame.size = label.frame.size;
imageView.frame = frame;
return imageView;
} else {
return nil;
}
}