Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding blocks to be delegate of textview on textFieldShouldReturn:

  • Loading branch information...
commit 76a37b87caabc6f8660dcddbbf187ff8dd392e42 1 parent 1a317a5
Diego Chohfi dchohfi authored
10 BlockAlertsDemo/BlockAlertsDemoViewController.m
View
@@ -25,7 +25,7 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (IBAction)showAlert:(id)sender
{
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Alert Title" message:@"This is a very long message, designed just to show you how smart this class is"];
-
+
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert setDestructiveButtonWithTitle:@"Kill!" block:nil];
[alert addButtonWithTitle:@"Show Action Sheet on top" block:^{
@@ -72,7 +72,7 @@ - (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)
@@ -80,13 +80,15 @@ - (IBAction)goNuts:(id)sender
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];
+ 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:^(UITextField *_textField){
+ [_textField resignFirstResponder];
+ }];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
7 BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h
View
@@ -8,6 +8,8 @@
#import "BlockAlertView.h"
+typedef void(^TextFieldCallBack)(UITextField *);
+
@interface BlockTextPromptAlertView : BlockAlertView <UITextFieldDelegate> {
NSCharacterSet *unacceptedInput;
@@ -17,11 +19,14 @@
@property (nonatomic, retain) UITextField *textField;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText;
++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldCallBack) block;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField;
++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldCallBack) block;
+
-- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText;
+- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldCallBack) block;
- (void)setAllowableCharacters:(NSString*)accepted;
41 BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m
View
@@ -14,28 +14,38 @@
#define kKeyboardResizeBounce 20
+@interface BlockTextPromptAlertView()
+@property(copy) TextFieldCallBack callBack;
+@end
@implementation BlockTextPromptAlertView
-@synthesize textField;
+@synthesize textField, callBack;
+
+
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText {
-
- return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText] autorelease];
-
+ return [self promptWithTitle:title message:message defaultText:defaultText block:nil];
+}
+
++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldCallBack)block {
+ return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText block:block] autorelease];
}
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField {
-
- BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil] autorelease];
+ return [self promptWithTitle:title message:message textField:textField block:nil];
+}
+
+
++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldCallBack) block{
+ BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil block:block] autorelease];
*textField = prompt.textField;
return prompt;
}
-
-- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText {
-
+- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldCallBack) block {
+
self = [super initWithTitle:title message:message];
if (self) {
@@ -50,11 +60,17 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(N
if (defaultText)
theTextField.text = defaultText;
+ if(block){
+ theTextField.delegate = self;
+ }
+
[_view addSubview:theTextField];
self.textField = theTextField;
_height += kTextBoxHeight + kTextBoxSpacing;
+
+ callBack = block;
}
return self;
@@ -111,6 +127,13 @@ - (void)setMaxLength:(NSInteger)max {
self.textField.delegate = self;
}
+-(BOOL)textFieldShouldReturn:(UITextField *)_textField{
+ if(callBack){
+ callBack(_textField);
+ }
+ return YES;
+}
+
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [self.textField.text length] + [string length] - range.length;
Please sign in to comment.
Something went wrong with that request. Please try again.