From 0a4cad0234cd86bfe8b5dee66491072debcfe175 Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:36:19 -0400 Subject: [PATCH 1/8] Added new method on SCLButton that accepts a title, action block, and validation block. --- SCLAlertView/SCLAlertView.h | 8 ++++++++ SCLAlertView/SCLAlertView.m | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/SCLAlertView/SCLAlertView.h b/SCLAlertView/SCLAlertView.h index 7e13a9c..e1a4503 100755 --- a/SCLAlertView/SCLAlertView.h +++ b/SCLAlertView/SCLAlertView.h @@ -94,6 +94,14 @@ typedef NS_ENUM(NSInteger, SCLAlertViewAnimation) */ - (SCLButton *)addButton:(NSString *)title actionBlock:(ActionBlock)action; +/** Add a Button with a title, a block to handle validation, and a block to handle when the button is pressed and validation succeeds. + * + * @param title The text displayed on the button. + * @param validationBlock A block of code that will allow you to validate fields or do any other logic you may want to do to determine if the alert should be dismissed or not. Inside of this block, return a BOOL indicating whether or not the action block should be called and the alert dismissed. + * @param actionBlock A block of code to be executed when the button is pressed and validation passes. + */ +- (SCLButton *)addButton:(NSString *)title validationBlock:(ValidationBlock)validationBlock actionBlock:(ActionBlock)action; + /** Add Button * * TODO diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m index 3786efb..34fe08d 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -323,6 +323,13 @@ - (SCLButton *)addButton:(NSString *)title actionBlock:(ActionBlock)action return btn; } +- (SCLButton *)addButton:(NSString *)title validationBlock:(ValidationBlock)validationBlock actionBlock:(ActionBlock)action +{ + SCLButton *btn = [self addButton:title actionBlock:action]; + btn.validationBlock = validationBlock; + + return btn; +} - (SCLButton *)addButton:(NSString *)title target:(id)target selector:(SEL)selector { From d149e1033c3d1f25a250db15ab5137538d53352a Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:36:53 -0400 Subject: [PATCH 2/8] Added new validation block property to SCLButton. --- SCLAlertView/SCLButton.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SCLAlertView/SCLButton.h b/SCLAlertView/SCLButton.h index c1a4c25..f5e0b77 100644 --- a/SCLAlertView/SCLButton.h +++ b/SCLAlertView/SCLButton.h @@ -11,6 +11,7 @@ @interface SCLButton : UIButton typedef void (^ActionBlock)(void); +typedef BOOL (^ValidationBlock)(void); // Action Types typedef NS_ENUM(NSInteger, SCLActionType) @@ -24,6 +25,7 @@ typedef NS_ENUM(NSInteger, SCLActionType) @property SCLActionType actionType; @property (nonatomic, copy) ActionBlock actionBlock; +@property (nonatomic, copy) ValidationBlock validationBlock; @property id target; From 49527148cecd6892ae49b88bf17006c9ab01571a Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:42:39 -0400 Subject: [PATCH 3/8] When the button is tapped and it has a validation block, run the block and if it fails don't continue with the button's action. --- SCLAlertView/SCLAlertView.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m index 34fe08d..c231849 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -344,6 +344,11 @@ - (SCLButton *)addButton:(NSString *)title target:(id)target selector:(SEL)selec - (void)buttonTapped:(SCLButton *)btn { + // If the button has a validation block, and the validation block returns NO, validation + // failed, so we should bail. + if (btn.validationBlock && btn.validationBlock() == NO) { + return; + } if (btn.actionType == Block) { if (btn.actionBlock) From 900bf48024f74c6976ebabdc67ad420f92e00f20 Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:43:24 -0400 Subject: [PATCH 4/8] Added validation demonstration to demo app. --- .../Base.lproj/Storyboard.storyboard | 10 +++- SCLAlertViewExample/ViewController.h | 1 + SCLAlertViewExample/ViewController.m | 53 ++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard index eb83715..fcd9725 100644 --- a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard +++ b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard @@ -1,7 +1,6 @@ - @@ -98,6 +97,15 @@ + diff --git a/SCLAlertViewExample/ViewController.h b/SCLAlertViewExample/ViewController.h index eb7f124..764fb8a 100644 --- a/SCLAlertViewExample/ViewController.h +++ b/SCLAlertViewExample/ViewController.h @@ -17,6 +17,7 @@ - (IBAction)showInfo:(id)sender; - (IBAction)showEdit:(id)sender; - (IBAction)showCustom:(id)sender; +- (IBAction)showValidation:(id)sender; @end diff --git a/SCLAlertViewExample/ViewController.m b/SCLAlertViewExample/ViewController.m index 8139ff4..cbe4c3b 100644 --- a/SCLAlertViewExample/ViewController.m +++ b/SCLAlertViewExample/ViewController.m @@ -138,9 +138,58 @@ - (IBAction)showWithDuration:(id)sender - (IBAction)showCustom:(id)sender { SCLAlertView *alert = [[SCLAlertView alloc] init]; - + UIColor *color = [UIColor colorWithRed:65.0/255.0 green:64.0/255.0 blue:144.0/255.0 alpha:1.0]; - [alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"OK" duration:0.0f]; + [alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"Cancel" duration:0.0f]; +} + +- (IBAction)showValidation:(id)sender { + SCLAlertView *alert = [[SCLAlertView alloc] init]; + + UITextField *evenField = [alert addTextField:@"Enter an even number"]; + evenField.keyboardType = UIKeyboardTypeNumberPad; + + UITextField *oddField = [alert addTextField:@"Enter an odd number"]; + oddField.keyboardType = UIKeyboardTypeNumberPad; + + [alert addButton:@"Test Validation" validationBlock:^BOOL{ + if (evenField.text.length == 0) { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"You forgot to add an even number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [evenField becomeFirstResponder]; + return NO; + } + + if (oddField.text.length == 0) { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"You forgot to add an odd number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [oddField becomeFirstResponder]; + return NO; + } + + NSInteger evenFieldEntry = [evenField.text integerValue]; + BOOL evenFieldPassedValidation = evenFieldEntry % 2 == 0; + + if (!evenFieldPassedValidation) { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"That is not an even number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [evenField becomeFirstResponder]; + return NO; + } + + NSInteger oddFieldEntry = [oddField.text integerValue]; + BOOL oddFieldPassedValidation = oddFieldEntry % 2 == 1; + + if (!oddFieldPassedValidation) { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"That is not an odd number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [oddField becomeFirstResponder]; + return NO; + } + + return YES; + + } actionBlock:^{ + [[[UIAlertView alloc] initWithTitle:@"Great Job!" message:@"Thanks for playing." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + }]; + + [alert showEdit:self title:@"Validation" subTitle:@"Ensure the data is correct before dismissing!" closeButtonTitle:@"Cancel" duration:0]; } - (void)firstButton From a0ed352ee8ed11f14dfff3f7a68eb6cef88dfe11 Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:49:56 -0400 Subject: [PATCH 5/8] Fixed unintended edit. --- SCLAlertViewExample/ViewController.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SCLAlertViewExample/ViewController.m b/SCLAlertViewExample/ViewController.m index cbe4c3b..f8ccb16 100644 --- a/SCLAlertViewExample/ViewController.m +++ b/SCLAlertViewExample/ViewController.m @@ -138,9 +138,9 @@ - (IBAction)showWithDuration:(id)sender - (IBAction)showCustom:(id)sender { SCLAlertView *alert = [[SCLAlertView alloc] init]; - + UIColor *color = [UIColor colorWithRed:65.0/255.0 green:64.0/255.0 blue:144.0/255.0 alpha:1.0]; - [alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"Cancel" duration:0.0f]; + [alert showCustom:self image:[UIImage imageNamed:@"git"] color:color title:@"Custom" subTitle:@"Add a custom icon and color for your own type of alert!" closeButtonTitle:@"OK" duration:0.0f]; } - (IBAction)showValidation:(id)sender { From d15102eca1a9165ed91e2f4fb79e48be5a6284cb Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Sat, 18 Oct 2014 20:55:45 -0400 Subject: [PATCH 6/8] Added validation info to README. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 166a57a..f8dba80 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,15 @@ alert.attributedFormatBlock = ^NSAttributedString* (NSString *value) NSLog(@"Second button tapped"); }]; +//Using Blocks With Validation +[alert addButton:@"Validate" validationBlock:^BOOL { + BOOL passedValidation = .... + return passedValidation; + +} actionBlock:^{ + // handle successful validation here +}]; + //Dismiss on tap outside (Default is NO) alert.shouldDismissOnTapOutside = YES; From f5ff59d743ddfa6ba6894bd93dbec1d88924539a Mon Sep 17 00:00:00 2001 From: Diogo Autilio Date: Mon, 20 Oct 2014 01:17:30 -0200 Subject: [PATCH 7/8] Set darker button background color on touch down event --- SCLAlertView/SCLAlertView.m | 2 +- SCLAlertView/SCLButton.h | 1 + SCLAlertView/SCLButton.m | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m index f0c025c..2488426 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -520,7 +520,7 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc image:(UIImage *)imag for (SCLButton *btn in _buttons) { - btn.backgroundColor = viewColor; + btn.defaultBackgroundColor = viewColor; if (style == Warning) { diff --git a/SCLAlertView/SCLButton.h b/SCLAlertView/SCLButton.h index f5e0b77..e365f73 100644 --- a/SCLAlertView/SCLButton.h +++ b/SCLAlertView/SCLButton.h @@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, SCLActionType) @property (nonatomic, copy) ActionBlock actionBlock; @property (nonatomic, copy) ValidationBlock validationBlock; +@property (nonatomic, strong) UIColor *defaultBackgroundColor; @property id target; diff --git a/SCLAlertView/SCLButton.m b/SCLAlertView/SCLButton.m index 8d87844..1412ea9 100644 --- a/SCLAlertView/SCLButton.m +++ b/SCLAlertView/SCLButton.m @@ -37,5 +37,46 @@ - (id)initWithFrame:(CGRect)frame return self; } +- (void)setHighlighted:(BOOL)highlighted +{ + if(highlighted) + { + self.backgroundColor = [self darkerColorForColor:_defaultBackgroundColor]; + } + else + { + self.backgroundColor = _defaultBackgroundColor; + } + [super setHighlighted:highlighted]; +} + +- (void)setDefaultBackgroundColor:(UIColor *)defaultBackgroundColor +{ + self.backgroundColor = _defaultBackgroundColor = defaultBackgroundColor; +} + +#pragma mark - Helpers + +- (UIColor *)darkerColorForColor:(UIColor *)color +{ + CGFloat r, g, b, a; + if ([color getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MAX(r - 0.2f, 0.0f) + green:MAX(g - 0.2f, 0.0f) + blue:MAX(b - 0.2f, 0.0f) + alpha:a]; + return nil; +} + +- (UIColor *)lighterColorForColor:(UIColor *)color +{ + CGFloat r, g, b, a; + if ([color getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MIN(r + 0.2f, 1.0f) + green:MIN(g + 0.2f, 1.0f) + blue:MIN(b + 0.2f, 1.0f) + alpha:a]; + return nil; +} @end From ff46af08a1eca7e022bb0de45a6b634b3c9044f3 Mon Sep 17 00:00:00 2001 From: Diogo Autilio Date: Mon, 20 Oct 2014 11:05:31 -0200 Subject: [PATCH 8/8] Bump version --- SCLAlertView-Objective-C.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SCLAlertView-Objective-C.podspec b/SCLAlertView-Objective-C.podspec index 6abfb11..83af3ed 100644 --- a/SCLAlertView-Objective-C.podspec +++ b/SCLAlertView-Objective-C.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SCLAlertView-Objective-C" - spec.version = "0.2.0" + spec.version = "0.2.1" spec.summary = "Beautiful animated Alert View. Written in Swift but ported to Objective-C" spec.homepage = "https://github.com/dogo/SCLAlertView" spec.screenshots = "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot.png", "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png" @@ -10,7 +10,7 @@ Pod::Spec.new do |spec| spec.social_media_url = "http://twitter.com/di_autilio" spec.platform = :ios spec.ios.deployment_target = '7.0' - spec.source = { :git => "https://github.com/dogo/SCLAlertView.git", :tag => "0.2.0" } + spec.source = { :git => "https://github.com/dogo/SCLAlertView.git", :tag => "0.2.1" } spec.source_files = "SCLAlertView/*" spec.requires_arc = true end