Skip to content
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

Added capability for additional text fields to be added to AlertViews #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Binary file added .DS_Store
Binary file not shown.
16 changes: 11 additions & 5 deletions BlockAlertsDemo/BlockAlertsDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,35 @@ - (IBAction)showActionSheetPlusAlert:(id)sender
- (IBAction)goNuts:(id)sender
{
for (int i=0; i<6; i++)
{
{
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.5 * i * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (arc4random() % 2 == 0)
[self showAlert:nil];
else
[self showActionSheet:nil];
});
}
}
}

- (IBAction)showTextPrompt:(id)sender
{
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle:@"Prompt Title" message:@"With prompts you do have to keep in mind limited screen space due to the keyboard" textField:&textField block:^(BlockTextPromptAlertView *alert){
//The number of placeholders passed in defines how many textfields will be added to the alertview
NSArray* placeHolderArray = [[[NSArray alloc] initWithObjects:@"TextField 1",@"TextField 2", @"TextField 3", nil] autorelease];
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle:@"Prompt Title" message:@"With prompts you do have to keep in mind limited screen space due to the keyboard" withPlaceholdersForTextFields:placeHolderArray block:^(BlockTextPromptAlertView *alert){
[alert.textField resignFirstResponder];
return YES;
}];


[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert addButtonWithTitle:@"Okay" block:^{
NSLog(@"Text: %@", textField.text);
NSMutableArray *textField = [alert getArrayOfTextInAlertViewTextFields];
int textfieldCount = [textField count];
int i;
for (i=0; i<textfieldCount; i++) {
NSLog(@"Textfield %d Text: %@", i,[textField objectAtIndex:i]);
}
}];
[alert show];
}
Expand Down
11 changes: 5 additions & 6 deletions BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@ typedef BOOL(^TextFieldReturnCallBack)(BlockTextPromptAlertView *);
NSInteger maxLength;
NSInteger buttonIndexForReturn;
NSString* defaultText;
NSArray* placeholders;
int numberOfPlaceholders;
}

@property (nonatomic, retain) UITextField *textField;

@property (nonatomic, assign) BOOL disableAutoBecomeFirstResponder;
@property (nonatomic, assign) BOOL selectAllOnBeginEdit;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldReturnCallBack) block;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message withPlaceholdersForTextFields:(NSArray*)placeHolderArray block:(TextFieldReturnCallBack) block;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldReturnCallBack) block;

- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText;

- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldReturnCallBack) block;
- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText textPlaceholders:(NSArray*)placeholderArray block: (TextFieldReturnCallBack) block;
- (NSMutableArray*)getArrayOfTextInAlertViewTextFields;

@property (readwrite, copy) BlockTextPromptAlertShouldDismiss shouldDismiss;

Expand Down
117 changes: 68 additions & 49 deletions BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,22 @@ @implementation BlockTextPromptAlertView
@synthesize textField, callBack;



+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText {
return [self promptWithTitle:title message:message defaultText:defaultText block:nil];
}

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldReturnCallBack)block {
return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText block:block] autorelease];
}

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField {
return [self promptWithTitle:title message:message textField:textField block:nil];
}


+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldReturnCallBack) block{
BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil block:block] autorelease];
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message withPlaceholdersForTextFields:(NSArray*)placeHolderArray block:(TextFieldReturnCallBack) block{

*textField = prompt.textField;
BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil textPlaceholders:placeHolderArray block:block] autorelease];

return prompt;
}

- (void)addComponents:(CGRect)frame {
[super addComponents:frame];
- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultTxt textPlaceholders:(NSArray*)placeholderArray block: (TextFieldReturnCallBack) block {
placeholders = placeholderArray;
self = [self initWithTitle:title message:message defaultText:defaultTxt];

if (!self.textField) {
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(kTextBoxHorizontalMargin, _height, frame.size.width - kTextBoxHorizontalMargin * 2, kTextBoxHeight)];

[theTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[theTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[theTextField setBorderStyle:UITextBorderStyleRoundedRect];
[theTextField setTextAlignment:NSTextAlignmentCenter];
[theTextField setClearButtonMode:UITextFieldViewModeAlways];

theTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;

theTextField.delegate = self;
theTextField.text = defaultText;

self.textField = theTextField;
}
else {
self.textField.frame = CGRectMake(kTextBoxHorizontalMargin, _height, frame.size.width - kTextBoxHorizontalMargin * 2, kTextBoxHeight);
if (self) {
self.callBack = block;
}

[_view addSubview:self.textField];
_height += kTextBoxHeight + kTextBoxSpacing;

return self;
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultTxt {
Expand All @@ -93,17 +61,65 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(N
return self;
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultTxt block: (TextFieldReturnCallBack) block {
#pragma mark - Set up text fields

- (void)addComponents:(CGRect)frame {
[super addComponents:frame];

self = [self initWithTitle:title message:message defaultText:defaultTxt];
numberOfPlaceholders = [placeholders count];
if (numberOfPlaceholders == 0) {
numberOfPlaceholders = 1;
[self createTextFieldWithPlaceholderAtIndex:numberOfPlaceholders withFrame:frame];
}
int i;
for (i = 0; i<numberOfPlaceholders; i++) {
[self createTextFieldWithPlaceholderAtIndex:i withFrame:frame];
}
}

- (void)createTextFieldWithPlaceholderAtIndex:(int)i withFrame:(CGRect)frame{
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(kTextBoxHorizontalMargin, _height, frame.size.width - kTextBoxHorizontalMargin * 2, kTextBoxHeight)];
NSString *placeholderString = [placeholders objectAtIndex:i];

if (self) {
self.callBack = block;
[theTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[theTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[theTextField setBorderStyle:UITextBorderStyleRoundedRect];
[theTextField setTextAlignment:NSTextAlignmentLeft];
[theTextField setClearButtonMode:UITextFieldViewModeAlways];

theTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;

theTextField.delegate = self;
theTextField.text = defaultText;
theTextField.placeholder = placeholderString;
theTextField.tag = i + 1; // We don't wan't the tag to ever be zero as it needs to be unique

[_view addSubview:theTextField];
_height += kTextBoxHeight + kTextBoxSpacing;
}

#pragma mark - Get text in created textfields

-(NSMutableArray*)getArrayOfTextInAlertViewTextFields{

NSMutableArray *searchCollection = [[[NSMutableArray alloc] init] autorelease];

int i;
for (i = 1; i<numberOfPlaceholders + 1; i++) {
if ([(UITextField *)[self.view viewWithTag:i] text] == NULL) {
[searchCollection addObject:@""];
}
else {

[searchCollection addObject:[(UITextField *)[self.view viewWithTag:i] text]];
}
}

return self;
return searchCollection;
}

#pragma mark - Button & keyboards methods

- (void)show {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
Expand Down Expand Up @@ -172,9 +188,15 @@ - (void)keyboardWillShow:(NSNotification *)notification {
}
completion:nil];
}

}


- (void)setButtonIndexForReturn:(NSInteger)index {
buttonIndexForReturn = index;
}

#pragma mark - Text Field Delegate Methods

- (void)setAllowableCharacters:(NSString*)accepted {
unacceptedInput = [[[NSCharacterSet characterSetWithCharactersInString:accepted] invertedSet] retain];
Expand All @@ -191,9 +213,6 @@ - (void)setMaxLength:(NSInteger)max {
self.textField.delegate = self;
}

- (void)setButtonIndexForReturn:(NSInteger)index {
buttonIndexForReturn = index;
}

-(BOOL)textFieldShouldReturn:(UITextField *)_textField{
if(callBack){
Expand Down Expand Up @@ -224,7 +243,7 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang

if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else
else
return YES;
}

Expand Down