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

default value for a textfield #295

Merged
merged 5 commits into from
Nov 22, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ alert.attributedFormatBlock = ^NSAttributedString* (NSString *value)
```Objective-C
SCLAlertView *alert = [[SCLAlertView alloc] init];

UITextField *textField = [alert addTextField:@"Enter your name"];
UITextField *textField = [alert addTextField:@"Enter your name" setDefaultText:nil];

[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
Expand Down
4 changes: 2 additions & 2 deletions SCLAlertView/SCLAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
*
* @param title The text displayed on the textfield.
*/
- (SCLTextView *)addTextField:(NSString *)title;
- (SCLTextView *)addTextField:(NSString *)title setDefaultText:(NSString *)defaultText;

/** Add a custom Text Field
*
Expand Down Expand Up @@ -567,7 +567,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
@property(copy, nonatomic) SCLAlertViewBuilder *(^alertShowAnimationIsCompleted) (SCLShowAnimationCompletionBlock showAnimationCompletionBlock);
@property(copy, nonatomic) SCLAlertViewBuilder *(^removeTopCircle)(void);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addCustomView)(UIView *view);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTextField)(NSString *title);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTextField)(NSString *title, NSString *defaultText);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addCustomTextField)(UITextField *textField);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addSwitchViewWithLabelTitle)(NSString *title);
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTimerToButtonIndex)(NSInteger buttonIndex, BOOL reverse);
Expand Down
25 changes: 21 additions & 4 deletions SCLAlertView/SCLAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ - (SCLSwitchView *)addSwitchViewWithLabel:(NSString *)label

#pragma mark - TextField

- (SCLTextView *)addTextField:(NSString *)title
- (SCLTextView *)addTextField:(NSString *)title setDefaultText:(NSString *)defaultText
{
[self addObservers];

Expand All @@ -577,6 +577,10 @@ - (SCLTextView *)addTextField:(NSString *)title
{
txt.placeholder = title;
}
if (defaultText != nil)
{
txt.text = defaultText;
}

[_contentView addSubview:txt];
[_inputs addObject:txt];
Expand Down Expand Up @@ -1637,12 +1641,14 @@ - (void)setupFluent {}
@interface SCLALertViewTextFieldBuilder()
#pragma mark - Parameters
@property(copy, nonatomic) NSString *parameterTitle;
@property(copy, nonatomic) NSString *parameterDefaultText;

#pragma mark - Available later after adding
@property(weak, nonatomic) SCLTextView *textField;

#pragma mark - Setters
@property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^title) (NSString *title);
@property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^defaultText) (NSString *defaultText);
@end

@implementation SCLALertViewTextFieldBuilder
Expand All @@ -1652,13 +1658,18 @@ - (void)setupFluent {
weakSelf.parameterTitle = title;
return weakSelf;
};
self.defaultText = ^(NSString *defaultText){
weakSelf.parameterDefaultText = defaultText;
return weakSelf;
};
}
@end

@interface SCLALertViewButtonBuilder()

#pragma mark - Parameters
@property(copy, nonatomic) NSString *parameterTitle;
@property(copy, nonatomic) NSString *parameterDefaultText;
@property(weak, nonatomic) id parameterTarget;
@property(assign, nonatomic) SEL parameterSelector;
@property(copy, nonatomic) void(^parameterActionBlock)(void);
Expand All @@ -1669,6 +1680,7 @@ @interface SCLALertViewButtonBuilder()

#pragma mark - Setters
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^title) (NSString *title);
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^defaultText) (NSString *defaultText);
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^target) (id target);
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^selector) (SEL selector);
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^actionBlock) (void(^actionBlock)(void));
Expand All @@ -1683,6 +1695,10 @@ - (void)setupFluent {
weakSelf.parameterTitle = title;
return weakSelf;
};
self.defaultText = ^(NSString *defaultText){
weakSelf.parameterDefaultText = defaultText;
return weakSelf;
};
self.target = ^(id target){
weakSelf.parameterTarget = target;
return weakSelf;
Expand Down Expand Up @@ -1822,8 +1838,8 @@ - (void)setupFluent {
[weakSelf.alertView addCustomView:view];
return weakSelf;
};
self.addTextField = ^(NSString *title) {
[weakSelf.alertView addTextField:title];
self.addTextField = ^(NSString *title, NSString *defaultText) {
[weakSelf.alertView addTextField:title setDefaultText:defaultText];
return weakSelf;
};
self.addCustomTextField = ^(UITextField *textField) {
Expand Down Expand Up @@ -1879,7 +1895,7 @@ - (void)setupFluent {
};

self.addTextFieldWithBuilder = ^(SCLALertViewTextFieldBuilder *builder){
builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle];
builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle setDefaultText:builder.parameterDefaultText];
return weakSelf;
};
}
Expand Down Expand Up @@ -1916,6 +1932,7 @@ @interface SCLAlertViewShowBuilder()
@property(copy, nonatomic) UIImage *parameterImage;
@property(copy, nonatomic) UIColor *parameterColor;
@property(copy, nonatomic) NSString *parameterTitle;
@property(copy, nonatomic) NSString *parameterDefaultText;
@property(copy, nonatomic) NSString *parameterSubTitle;
@property(copy, nonatomic) NSString *parameterCompleteText;
@property(copy, nonatomic) NSString *parameterCloseButtonTitle;
Expand Down
16 changes: 8 additions & 8 deletions SCLAlertViewExample/ViewController/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ - (IBAction)showEdit:(id)sender
{
SCLAlertView *alert = [[SCLAlertView alloc] init];

SCLTextView *textField = [alert addTextField:@"Enter your name"];
SCLTextView *textField = [alert addTextField:@"Enter your name" setDefaultText:nil];
[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
}];
Expand All @@ -132,7 +132,7 @@ - (IBAction)showEditWithHorizontalButtons:(id)sender
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert setHorizontalButtons:YES];

SCLTextView *textField = [alert addTextField:@"Enter your name"];
SCLTextView *textField = [alert addTextField:@"Enter your name" setDefaultText:nil];
alert.hideAnimationType = SCLAlertViewHideAnimationSimplyDisappear;
[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
Expand All @@ -157,7 +157,7 @@ - (IBAction)showAdvanced:(id)sender
NSLog(@"Second button tapped");
}];

SCLTextView *textField = [alert addTextField:@"Enter your name"];
SCLTextView *textField = [alert addTextField:@"Enter your name" setDefaultText:nil];

[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
Expand Down Expand Up @@ -211,7 +211,7 @@ - (IBAction)ShowAdvancedWithHorizontalButtons:(id)sender
NSLog(@"Second button tapped");
}];

SCLTextView *textField = [alert addTextField:@"Enter your name"];
SCLTextView *textField = [alert addTextField:@"Enter your name" setDefaultText:nil];

[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
Expand Down Expand Up @@ -267,10 +267,10 @@ - (IBAction)showValidation:(id)sender
{
SCLAlertView *alert = [[SCLAlertView alloc] init];

SCLTextView *evenField = [alert addTextField:@"Enter an even number"];
SCLTextView *evenField = [alert addTextField:@"Enter an even number" setDefaultText:nil];
evenField.keyboardType = UIKeyboardTypeNumberPad;

SCLTextView *oddField = [alert addTextField:@"Enter an odd number"];
SCLTextView *oddField = [alert addTextField:@"Enter an odd number" setDefaultText:nil];
oddField.keyboardType = UIKeyboardTypeNumberPad;

__weak __typeof(self) weakSelf = self;
Expand Down Expand Up @@ -326,10 +326,10 @@ - (IBAction)showValidationWithHorizontalButtons:(id)sender
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert setHorizontalButtons:YES];

SCLTextView *evenField = [alert addTextField:@"Enter an even number"];
SCLTextView *evenField = [alert addTextField:@"Enter an even number" setDefaultText:nil];
evenField.keyboardType = UIKeyboardTypeNumberPad;

SCLTextView *oddField = [alert addTextField:@"Enter an odd number"];
SCLTextView *oddField = [alert addTextField:@"Enter an odd number" setDefaultText:nil];
oddField.keyboardType = UIKeyboardTypeNumberPad;

__weak __typeof(self) weakSelf = self;
Expand Down