Skip to content

Commit

Permalink
Adding optional blank checking for each created UITextField
Browse files Browse the repository at this point in the history
  • Loading branch information
hellhound committed Sep 2, 2011
1 parent cedfe68 commit 3fac33f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 43 deletions.
37 changes: 22 additions & 15 deletions TSAlertView/TSAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,36 @@ didDismissWithButtonIndex:(NSInteger)buttonIndex;
NSInteger _firstOtherButtonIndex;
CGFloat _width;
CGFloat _maxHeight;
NSUInteger _shouldNotAdmitBlanks;
BOOL _usesMessageTextView;
TSAlertViewStyle _style;
// TSAlertView (TSCustomizableAlertView)
NSMutableArray *_textFields;
id _userInfo;
}

@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *message;
@property(nonatomic, assign) id<TSAlertViewDelegate> delegate;
@property(nonatomic, assign) NSInteger cancelButtonIndex;
@property(nonatomic, readonly) NSInteger firstOtherButtonIndex;
@property(nonatomic, readonly) NSInteger numberOfButtons;
@property(nonatomic, readonly, getter=isVisible) BOOL visible;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *message;
@property (nonatomic, assign) id<TSAlertViewDelegate> delegate;
@property (nonatomic, assign) NSInteger cancelButtonIndex;
@property (nonatomic, readonly) NSInteger firstOtherButtonIndex;
@property (nonatomic, readonly) NSInteger numberOfButtons;
@property (nonatomic, readonly, getter=isVisible) BOOL visible;
// shouldNotAdmitBlanks are flags, for each UITextField, from lest significant
// (i.e. [_textFields objectAtIndex:0]) to most significant bit
// (i.e. [_textFIelds objectAtIndex:n - 1]).
// By default shouldNotAdmitBlanks has its flags set for each initialzed
// UITextField
@property (nonatomic, assign) NSUInteger shouldNotAdmitBlanks;
@property (nonatomic, assign) BOOL usesMessageTextView;

@property(nonatomic, assign) TSAlertViewButtonLayout buttonLayout;
@property(nonatomic, assign) CGFloat width;
@property(nonatomic, assign) CGFloat maxHeight;
@property(nonatomic, assign) BOOL usesMessageTextView;
@property(nonatomic, retain) UIImage *backgroundImage;
@property(nonatomic, assign) TSAlertViewStyle style;
@property(nonatomic, readonly) UITextField *inputTextField;
@property(nonatomic, retain) id userInfo;
@property (nonatomic, assign) TSAlertViewButtonLayout buttonLayout;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat maxHeight;
@property (nonatomic, retain) UIImage *backgroundImage;
@property (nonatomic, assign) TSAlertViewStyle style;
@property (nonatomic, readonly) UITextField *inputTextField;
@property (nonatomic, retain) id userInfo;

- (id)initWithTitle:(NSString *)title
message:(NSString *)message
Expand Down
101 changes: 73 additions & 28 deletions TSAlertView/TSAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
const CGFloat kTSAlertView_RowMargin = 5.;
const CGFloat kTSAlertView_ColumnMargin = 10.;

@interface NSString (TSString)

- (BOOL)isBlank;
@end

@implementation NSString (TSString)

- (BOOL)isBlank
{
static NSCharacterSet *whitespaceCharSet = nil;

if (whitespaceCharSet == nil)
whitespaceCharSet =
[[NSCharacterSet whitespaceAndNewlineCharacterSet] retain];

return [@"" isEqualToString:[self stringByTrimmingCharactersInSet:
whitespaceCharSet]];
}
@end

@interface TSAlertOverlayWindow : UIWindow
{
UIWindow *_oldKeyWindow;
Expand Down Expand Up @@ -475,7 +495,6 @@ - (CGSize)recalcSizeAndLayout:(BOOL)layout
//$$what if it's still too tall?
messageViewSize.height = [self maxHeight] - totalHeight;
totalHeight = [self maxHeight];
[self setUsesMessageTextView:YES];
}
if (layout) {
// title
Expand Down Expand Up @@ -616,9 +635,10 @@ - (void)setTextFieldProperties:(UITextField **)textField
@synthesize cancelButtonIndex = _cancelButtonIndex;
@synthesize buttonLayout = _buttonLayout;
@synthesize firstOtherButtonIndex = _firstOtherButtonIndex;
@synthesize shouldNotAdmitBlanks = _shouldNotAdmitBlanks;
@synthesize usesMessageTextView = _usesMessageTextView;
@synthesize width = _width;
@synthesize maxHeight = _maxHeight;
@synthesize usesMessageTextView = _usesMessageTextView;
@synthesize style = _style;
@synthesize userInfo = _userInfo;

Expand All @@ -633,6 +653,7 @@ - (id)initWithTitle:(NSString *)title
[self setTitle:title];
[self setMessage:message];
[self setDelegate:delegate];
[self setUsesMessageTextView:YES];
if (nil != cancelButtonTitle) {
[self addButtonWithTitle:cancelButtonTitle];
[self setCancelButtonIndex:.0];
Expand Down Expand Up @@ -708,9 +729,8 @@ - (UITextField *)inputTextField
if (_textFields == nil) {
UITextField *inputTextField = [[[UITextField alloc] init] autorelease];

[self setTextFieldProperties:&inputTextField];
_textFields =
[[NSMutableArray alloc] initWithObjects:inputTextField, nil];
_textFields = [[NSMutableArray alloc] init];
[self addTextField:inputTextField];
}
return [_textFields objectAtIndex:0];
}
Expand Down Expand Up @@ -823,28 +843,48 @@ - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex
animated:(BOOL)animated
{
if ([self style] == TSAlertViewStyleInput &&
[[self inputTextField] isFirstResponder])
[[self inputTextField] resignFirstResponder];
if ([[self delegate] respondsToSelector:
@selector(alertView:willDismissWithButtonIndex:)])
[[self delegate] alertView:self willDismissWithButtonIndex:buttonIndex];
if (animated) {
[[self window] setBackgroundColor:[UIColor clearColor]];
[[self window] setAlpha:1.];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.2];
[[self window] resignKeyWindow];
[[self window] setAlpha:.0];
[self releaseWindow:buttonIndex];
[UIView commitAnimations];
} else {
[[self window] resignKeyWindow];
[self releaseWindow:buttonIndex];
BOOL shouldContinue = YES;
NSUInteger shouldNotAdmitBlanks = [self shouldNotAdmitBlanks];

if ([self style] == TSAlertViewStyleInput && shouldNotAdmitBlanks) {
NSArray *textFields = [self textFields];
NSUInteger i, count = [textFields count], mask = (NSUInteger)-1;

for (i = 0; i < count; i++) {
if (!(mask & shouldNotAdmitBlanks >> i))
continue;

UITextField *textField = [textFields objectAtIndex:i];
NSString *text = [textField text];

if (text == nil || [text isBlank]) {
shouldContinue = NO;
break;
}
}
}
if (shouldContinue) {
if ([[self delegate] respondsToSelector:
@selector(alertView:willDismissWithButtonIndex:)])
[[self delegate] alertView:self
willDismissWithButtonIndex:buttonIndex];
if (animated) {
[[self window] setBackgroundColor:[UIColor clearColor]];
[[self window] setAlpha:1.];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.2];
[[self window] resignKeyWindow];
[[self window] setAlpha:.0];
[self releaseWindow:buttonIndex];
[UIView commitAnimations];
} else {
[[self window] resignKeyWindow];
[self releaseWindow:buttonIndex];
}
// Force keyboard dismissal
[[self textFields] makeObjectsPerformSelector:
@selector(resignFirstResponder)];
}
// Force keyboard dismissal
[[self textFields] makeObjectsPerformSelector:
@selector(resignFirstResponder)];
}

- (void)show
Expand Down Expand Up @@ -909,9 +949,14 @@ - (UITextField *)firstTextField

- (void)addTextField:(UITextField *)textField
{
//[textField setBackgroundColor:[UIColor clearColor]];
NSMutableArray *textFields = [self textFields];

[self setTextFieldProperties:&textField];
[[self textFields] addObject:textField];
// Set shouldNotAdmitBlanks correspondent flag for this UITextField
[self setShouldNotAdmitBlanks:
[self shouldNotAdmitBlanks] + (1 << [textFields count])];
[textFields addObject:textField];

}

- (UITextField *)addTextFieldWithLabel:(NSString *)label
Expand Down

0 comments on commit 3fac33f

Please sign in to comment.