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; 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 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 01b5c40..2488426 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -355,6 +355,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 { @@ -369,6 +376,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) @@ -508,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 c1a4c25..e365f73 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,8 @@ typedef NS_ENUM(NSInteger, SCLActionType) @property SCLActionType actionType; @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 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..f8ccb16 100644 --- a/SCLAlertViewExample/ViewController.m +++ b/SCLAlertViewExample/ViewController.m @@ -143,6 +143,55 @@ - (IBAction)showCustom:(id)sender { [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 { + 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 { NSLog(@"First button tapped");