Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions XCDFormInputAccessoryView/XCDFormInputAccessoryView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, XCDFormInputAccessoryViewAction) {
XCDFormInputAccessoryViewActionNext,
XCDFormInputAccessoryViewActionPrevious,
XCDFormInputAccessoryViewActionDone
};

typedef BOOL (^XCDFormInputAccessoryViewActionBlock)(XCDFormInputAccessoryViewAction action);

@interface XCDFormInputAccessoryView : UIView

/**
Expand All @@ -25,6 +33,15 @@

@property (nonatomic, assign) BOOL hasDoneButton;

@property (nonatomic, strong) UIColor *tintColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *barTintColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, copy) XCDFormInputAccessoryViewActionBlock actionHandler;

- (void) selectAdjacentResponderAtIndex:(NSInteger)index;
- (void) previous;
- (void) next;
- (void) done;

- (void) setHasDoneButton:(BOOL)hasDoneButton animated:(BOOL)animated;

@end
66 changes: 57 additions & 9 deletions XCDFormInputAccessoryView/XCDFormInputAccessoryView.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ - (instancetype) initWithResponders:(NSArray *)responders tintColor:(UIColor *)t
return nil;

_responders = responders;

self.backgroundColor = [UIColor clearColor];

self.toolbar = [[UIToolbar alloc] init];
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Expand Down Expand Up @@ -168,7 +170,7 @@ - (void) setHasDoneButton:(BOOL)hasDoneButton animated:(BOOL)animated

NSArray *items;
if (hasDoneButton)
items = [self.toolbar.items arrayByAddingObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)]];
items = [self.toolbar.items arrayByAddingObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)]];
else
items = [self.toolbar.items subarrayWithRange:NSMakeRange(0, 2)];

Expand All @@ -188,27 +190,73 @@ - (void) selectAdjacentResponderAtIndex:(NSInteger)index
adjacentResponder = [self.responders objectAtIndex:adjacentResponderIndex];

// Resign the previous responder before selecting the next one, so the UIKeyboard events could be notified properly.
[firstResponder resignFirstResponder];
// Only resign if < iOS 7
if (!(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)) {
[firstResponder resignFirstResponder];
}

[adjacentResponder becomeFirstResponder];
}

- (void) previous:(UIBarButtonItem *)sender
{
- (void) previous:(UIBarButtonItem *)sender {
BOOL shouldPrevious = YES;

if (self.actionHandler) {
shouldPrevious = self.actionHandler(XCDFormInputAccessoryViewActionPrevious);
}

if (shouldPrevious) {
[self previous];
}
}

- (void) next:(UIBarButtonItem *)sender {
BOOL shouldNext = YES;

if (self.actionHandler) {
shouldNext = self.actionHandler(XCDFormInputAccessoryViewActionNext);
}

if (shouldNext) {
[self next];
}
}

- (void) done:(UIBarButtonItem *)sender {
BOOL shouldDone = YES;

if (self.actionHandler) {
shouldDone = self.actionHandler(XCDFormInputAccessoryViewActionDone);
}

if (shouldDone) {
[self done];
}
}

- (void) previous {
[self selectAdjacentResponderAtIndex:0];
}

- (void) next:(UIBarButtonItem *)sender
{
- (void) next {
[self selectAdjacentResponderAtIndex:1];
}

- (void) done
{
- (void) done {
UIResponder *firstResponder = [self firstResponder];
[firstResponder resignFirstResponder];

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)setTintColor:(UIColor *)tintColor {
_tintColor = tintColor;
self.toolbar.tintColor = tintColor;
}

- (void)setBarTintColor:(UIColor *)barTintColor {
_barTintColor = barTintColor;
self.toolbar.barTintColor = barTintColor;
}

@end