Permalink
Browse files

Merge pull request #18 from dchohfi/master

Adding support to BlockTextPromptAlertView textField delegate using block
  • Loading branch information...
gpambrozio committed Apr 23, 2012
2 parents 1a317a5 + be51f4c commit 4999c4ab00c2d5085b3d42dd5bc9cdd91c801f33
@@ -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,21 +72,24 @@ - (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];
+ 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){
+ [alert.textField resignFirstResponder];
+ return YES;
+ }];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
@@ -8,6 +8,10 @@
#import "BlockAlertView.h"
+@class BlockTextPromptAlertView;
+
+typedef BOOL(^TextFieldReturnCallBack)(BlockTextPromptAlertView *);
+
@interface BlockTextPromptAlertView : BlockAlertView <UITextFieldDelegate> {
NSCharacterSet *unacceptedInput;
@@ -17,11 +21,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:(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;
- (void)setAllowableCharacters:(NSString*)accepted;
@@ -14,28 +14,38 @@
#define kKeyboardResizeBounce 20
+@interface BlockTextPromptAlertView()
+@property(nonatomic, copy) TextFieldReturnCallBack 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:(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 {
-
- 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:(TextFieldReturnCallBack) 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: (TextFieldReturnCallBack) block {
+
self = [super initWithTitle:title message:message];
if (self) {
@@ -50,16 +60,21 @@ - (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;
+
+ self.callBack = block;
}
return self;
}
-
- (void)show {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
@@ -111,6 +126,13 @@ - (void)setMaxLength:(NSInteger)max {
self.textField.delegate = self;
}
+-(BOOL)textFieldShouldReturn:(UITextField *)_textField{
+ if(callBack){
+ return callBack(self);
+ }
+ return NO;
+}
+
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [self.textField.text length] + [string length] - range.length;
@@ -127,4 +149,10 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
return YES;
}
+- (void)dealloc
+{
+ self.callBack = nil;
+ [super dealloc];
+}
+
@end

0 comments on commit 4999c4a

Please sign in to comment.