Skip to content

Add validation block #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 19, 2014
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions SCLAlertView/SCLAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions SCLAlertView/SCLAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -337,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)
Expand Down
2 changes: 2 additions & 0 deletions SCLAlertView/SCLButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface SCLButton : UIButton

typedef void (^ActionBlock)(void);
typedef BOOL (^ValidationBlock)(void);

// Action Types
typedef NS_ENUM(NSInteger, SCLActionType)
Expand All @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion SCLAlertViewExample/Base.lproj/Storyboard.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6245" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="Ohq-jH-G4R">
<dependencies>
<deployment defaultVersion="1792" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -98,6 +97,15 @@
<action selector="showCustom:" destination="Ohq-jH-G4R" eventType="touchUpInside" id="m4v-4B-1YQ"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XBG-LD-1eZ">
<rect key="frame" x="88" y="466" width="143" height="30"/>
<state key="normal" title="Show Validation">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showValidation:" destination="Ohq-jH-G4R" eventType="touchUpInside" id="1DV-xd-ugz"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand Down
1 change: 1 addition & 0 deletions SCLAlertViewExample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- (IBAction)showInfo:(id)sender;
- (IBAction)showEdit:(id)sender;
- (IBAction)showCustom:(id)sender;
- (IBAction)showValidation:(id)sender;

@end

49 changes: 49 additions & 0 deletions SCLAlertViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down