Skip to content

Commit df84fa2

Browse files
authored
Merge pull request dogo#295 from syto203/develop
default value for a textfield
2 parents 4cc166e + 531cb22 commit df84fa2

4 files changed

Lines changed: 32 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ alert.attributedFormatBlock = ^NSAttributedString* (NSString *value)
178178
```Objective-C
179179
SCLAlertView *alert = [[SCLAlertView alloc] init];
180180

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

183183
[alert addButton:@"Show Name" actionBlock:^(void) {
184184
NSLog(@"Text value: %@", textField.text);

SCLAlertView/SCLAlertView.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
289289
*
290290
* @param title The text displayed on the textfield.
291291
*/
292-
- (SCLTextView *)addTextField:(NSString *)title;
292+
- (SCLTextView *)addTextField:(NSString *)title setDefaultText:(NSString *)defaultText;
293293

294294
/** Add a custom Text Field
295295
*
@@ -567,7 +567,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
567567
@property(copy, nonatomic) SCLAlertViewBuilder *(^alertShowAnimationIsCompleted) (SCLShowAnimationCompletionBlock showAnimationCompletionBlock);
568568
@property(copy, nonatomic) SCLAlertViewBuilder *(^removeTopCircle)(void);
569569
@property(copy, nonatomic) SCLAlertViewBuilder *(^addCustomView)(UIView *view);
570-
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTextField)(NSString *title);
570+
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTextField)(NSString *title, NSString *defaultText);
571571
@property(copy, nonatomic) SCLAlertViewBuilder *(^addCustomTextField)(UITextField *textField);
572572
@property(copy, nonatomic) SCLAlertViewBuilder *(^addSwitchViewWithLabelTitle)(NSString *title);
573573
@property(copy, nonatomic) SCLAlertViewBuilder *(^addTimerToButtonIndex)(NSInteger buttonIndex, BOOL reverse);

SCLAlertView/SCLAlertView.m

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ - (SCLSwitchView *)addSwitchViewWithLabel:(NSString *)label
561561

562562
#pragma mark - TextField
563563

564-
- (SCLTextView *)addTextField:(NSString *)title
564+
- (SCLTextView *)addTextField:(NSString *)title setDefaultText:(NSString *)defaultText
565565
{
566566
[self addObservers];
567567

@@ -577,6 +577,10 @@ - (SCLTextView *)addTextField:(NSString *)title
577577
{
578578
txt.placeholder = title;
579579
}
580+
if (defaultText != nil)
581+
{
582+
txt.text = defaultText;
583+
}
580584

581585
[_contentView addSubview:txt];
582586
[_inputs addObject:txt];
@@ -1637,12 +1641,14 @@ - (void)setupFluent {}
16371641
@interface SCLALertViewTextFieldBuilder()
16381642
#pragma mark - Parameters
16391643
@property(copy, nonatomic) NSString *parameterTitle;
1644+
@property(copy, nonatomic) NSString *parameterDefaultText;
16401645

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

16441649
#pragma mark - Setters
16451650
@property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^title) (NSString *title);
1651+
@property(copy, nonatomic) SCLALertViewTextFieldBuilder *(^defaultText) (NSString *defaultText);
16461652
@end
16471653

16481654
@implementation SCLALertViewTextFieldBuilder
@@ -1652,13 +1658,18 @@ - (void)setupFluent {
16521658
weakSelf.parameterTitle = title;
16531659
return weakSelf;
16541660
};
1661+
self.defaultText = ^(NSString *defaultText){
1662+
weakSelf.parameterDefaultText = defaultText;
1663+
return weakSelf;
1664+
};
16551665
}
16561666
@end
16571667

16581668
@interface SCLALertViewButtonBuilder()
16591669

16601670
#pragma mark - Parameters
16611671
@property(copy, nonatomic) NSString *parameterTitle;
1672+
@property(copy, nonatomic) NSString *parameterDefaultText;
16621673
@property(weak, nonatomic) id parameterTarget;
16631674
@property(assign, nonatomic) SEL parameterSelector;
16641675
@property(copy, nonatomic) void(^parameterActionBlock)(void);
@@ -1669,6 +1680,7 @@ @interface SCLALertViewButtonBuilder()
16691680

16701681
#pragma mark - Setters
16711682
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^title) (NSString *title);
1683+
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^defaultText) (NSString *defaultText);
16721684
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^target) (id target);
16731685
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^selector) (SEL selector);
16741686
@property(copy, nonatomic) SCLALertViewButtonBuilder *(^actionBlock) (void(^actionBlock)(void));
@@ -1683,6 +1695,10 @@ - (void)setupFluent {
16831695
weakSelf.parameterTitle = title;
16841696
return weakSelf;
16851697
};
1698+
self.defaultText = ^(NSString *defaultText){
1699+
weakSelf.parameterDefaultText = defaultText;
1700+
return weakSelf;
1701+
};
16861702
self.target = ^(id target){
16871703
weakSelf.parameterTarget = target;
16881704
return weakSelf;
@@ -1822,8 +1838,8 @@ - (void)setupFluent {
18221838
[weakSelf.alertView addCustomView:view];
18231839
return weakSelf;
18241840
};
1825-
self.addTextField = ^(NSString *title) {
1826-
[weakSelf.alertView addTextField:title];
1841+
self.addTextField = ^(NSString *title, NSString *defaultText) {
1842+
[weakSelf.alertView addTextField:title setDefaultText:defaultText];
18271843
return weakSelf;
18281844
};
18291845
self.addCustomTextField = ^(UITextField *textField) {
@@ -1879,7 +1895,7 @@ - (void)setupFluent {
18791895
};
18801896

18811897
self.addTextFieldWithBuilder = ^(SCLALertViewTextFieldBuilder *builder){
1882-
builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle];
1898+
builder.textField = [weakSelf.alertView addTextField:builder.parameterTitle setDefaultText:builder.parameterDefaultText];
18831899
return weakSelf;
18841900
};
18851901
}
@@ -1916,6 +1932,7 @@ @interface SCLAlertViewShowBuilder()
19161932
@property(copy, nonatomic) UIImage *parameterImage;
19171933
@property(copy, nonatomic) UIColor *parameterColor;
19181934
@property(copy, nonatomic) NSString *parameterTitle;
1935+
@property(copy, nonatomic) NSString *parameterDefaultText;
19191936
@property(copy, nonatomic) NSString *parameterSubTitle;
19201937
@property(copy, nonatomic) NSString *parameterCompleteText;
19211938
@property(copy, nonatomic) NSString *parameterCloseButtonTitle;

SCLAlertViewExample/ViewController/ViewController.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ - (IBAction)showEdit:(id)sender
119119
{
120120
SCLAlertView *alert = [[SCLAlertView alloc] init];
121121

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

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

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

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

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

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

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

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

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

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

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

335335
__weak __typeof(self) weakSelf = self;

0 commit comments

Comments
 (0)