diff --git a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h index f0c79d0..0acbe54 100644 --- a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h +++ b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h @@ -7,6 +7,14 @@ #import +typedef NS_ENUM(NSInteger, XCDFormInputAccessoryViewAction) { + XCDFormInputAccessoryViewActionNext, + XCDFormInputAccessoryViewActionPrevious, + XCDFormInputAccessoryViewActionDone +}; + +typedef BOOL (^XCDFormInputAccessoryViewActionBlock)(XCDFormInputAccessoryViewAction action); + @interface XCDFormInputAccessoryView : UIView /** @@ -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 diff --git a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m index deb715e..38358d3 100644 --- a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m +++ b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m @@ -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; @@ -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)]; @@ -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