how do i fire textFieldDidEndEditing function for a textfield created programmatically - ios5

The following is the code that i am writing
UITextField *category =[[UITextField alloc] initWithFrame:CGRectMake(10, 90, subView.frame.size.width-15, 30)];
category.backgroundColor=[UIColor whiteColor];
category.placeholder = #"Category, Description, Model (optional)";
category.returnKeyType = UIReturnKeyNext;
// [category addTarget:self action:#selector(textFeild) forControlEvents:<#(UIControlEvents)#>]
What should the statement be so that I can add the textFieldDidEndEditing control event?

Set the delegate of UITextField to self
category.delegate = self;
Make sure your class which creates the UITextField implements the UITextFieldDelegate

Related

How to work with UITextFields inside of UIActionsheet?

i am new in iphone.i am doing reminder application to my project . i want get input values like reminder title and data&Time from uiactionsheet.i have created datepicker and uitextfield in actionsheet . The actionsheet appears with textfield.when i clicked on textfield,the keypad appears but not working means..when i was typed they are not visible in the textfield
-(void)btnSetRemainderTapped:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select reminder date and time"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Save"
otherButtonTitles:nil,nil];
menu.tag =10;
[menu showInView:self.view];
menu.userInteractionEnabled=YES;
UITextField *txtReminderTitle= [[UITextField alloc] initWithFrame:CGRectMake(20.0, 180, 280.0, 40.0)];
[txtReminderTitle setTag:99];
[txtReminderTitle setBackgroundColor:[UIColor whiteColor]];
[txtReminderTitle setFont:[UIFont boldSystemFontOfSize:17]];
[txtReminderTitle setBorderStyle:UITextBorderStyleNone];
[txtReminderTitle setTextAlignment:UITextAlignmentCenter];
txtReminderTitle.borderStyle = UITextBorderStyleRoundedRect;
txtReminderTitle.keyboardType = UIKeyboardTypeDefault;
txtReminderTitle.returnKeyType = UIReturnKeyDone;
txtReminderTitle.delegate =self;
[menu addSubview:txtReminderTitle];
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.tag =9999;
[menu addSubview:pickerView];
CGRect menuRect = menu.frame;
NSInteger orgHeight = menuRect.size.height;
menuRect.origin.y -= 270; //height of picker
menuRect.size.height = orgHeight+270;
menu.frame = menuRect;
CGRect pickerRect = CGRectMake(20, 250, 280, 200);
pickerView.frame = pickerRect;
}
-(void) actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:9999];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterLongStyle;
[df setTimeStyle:NSDateFormatterShortStyle];
UITextField * txtReminderTitle = (UITextField *)[actionSheet viewWithTag:99];
NSLog(#"%#",txtReminderTitle.text); //it print null value
}
I'm not sure why it's not accepting input from the keyboard. Any suggestions?
I found that, when you add UITextField to UIActionSheet then UITextField is remain under the UIActionSheet. So you need to bring UITextField above UIActionSheet thats way
[menu addSubview:txtReminderTitle]; does not work for UITextField to input any text.
Use following code for add UITextField:
UIWindow *appWindow = [UIApplication sharedApplication].keyWindow;
[appWindow insertSubview:txtReminderTitle aboveSubview:menu];
Only need to do changes in how to add UITextField.
Use custom UIActionSheet . do like following
1) add new UIViewController
2) declare one UITextField
#interface ViewController
{
UITextField *TextField;
}
3) add delegates of ActionSheet & TextField
#interface ViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> {
UITextField *TextField;
}
4)Now implement methods
see this link just i see on stackoverflow

I can't input in the second UITextField~~ Any one can teach me?

I make a UIViewController. It's a Sign in window. I use presentModalViewController method to pop this Sign in window. In this Sign in window's view, i put two UITextField with simply code, a username textfield and a password textfield. But when i run this app, a bug appeared. I tap one textfield optional, it can input at will. And then, i tap another textfield, i can't input anything any more.
UITextField * f1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 25)];
[f1 setBorderStyle:UITextBorderStyleRoundedRect];
UITextField * f2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 100, 25)];
[f2 setBorderStyle:UITextBorderStyleRoundedRect];
[[self view] addSubview:f1];
[[self view] addSubview:f2];
Have anyone met this situation? please help me, thanks all.
Give the fields a delegate connection with
[f1 setDelegate:self];
In your .h file pit the
#interface SomeViewController : UIViewController<UITextFieldDelegate>
{
}
-(IBAction)textFieldDone:(id)sender;
#end
and for the fields:
-(void)viewDidLoad{
UITextField * f1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 170.0, 300, 25)];
f1.borderStyle = UITextBorderStyleRoundedRect;
f1.text = #"";
[f1 setDelegate:self];
[f1 addTarget:self action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:ke ti kazam zosto na telefon];
UITextField * f2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 210, 300, 25)];
f2.borderStyle = UITextBorderStyleRoundedRect;
f2.text = #"";
[f2 setDelegate:self];
[f2 addTarget:self action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:f2];
}
-(IBAction)textFieldDone:(id)sender{
NSLog(#"textFieldDone");
}
hope this helps you.
I cant answer your question unfortunately but I can guide you to a tutorial that might help you out http://www.youtube.com/watch?v=h6MsPFPLeLY

how to acces the textfield value created manually from a Click on Button

i am doing sample application where i am creating two TextField programitically i.e,Textfield1 and Textfield2 and Having one Button which is Dragged on UI.
Now My requirement is I have two textfield created manually and a Button, When i click a button it should access the value entered in both textfield and display its value on console using NSLog.
so can anyone suggest me how to do it?As textifeild is created programmitically it is not having IBOutlet. so i want to read textfield value when i click button and display on Console.so please suggest me with sample code
{
[self Textfiled1];
[Self TextField2];
}
-(void)textField1
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:textField];
[textField release];
}
Same as above for Textfield2.
Set tag for both the UITextField.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 1;
...
for the second,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 2;
...
In Button click action method,
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *text1 = textField.text;
Similarly for the next UITextField.
Set the text field' s tag property before adding it and get the text field back using it.
textField.tag = 1;
UITextField* tf1 = (UITextField*)[self.view viewWithTag:1];
when you are adding textfield to self.view use the tag like below.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
textField.tag = 3;
[self.view addSubview:textField];
[textField release];
whenever you need it then do like this.
UITextField *tex = [self.view viewWithTag:3];
NSLog(#"%#",tex.text);

UITextField embedded in an UIView does not respond to touch events

In my project i'm using a UITextField which is embedded into a plain white UIButton with rounded corners in order to create a "beautiful" text field. This code is used several times in different views so I decided to create a custom UIView for this purpose. The code for this custom UIView:
BSCustomTextField.h
#import <Foundation/Foundation.h>
#interface BSCustomTextField : UIView {
UIButton* btnTextFieldContainer;
UITextField* textField;
NSString* text;
}
#property (retain, nonatomic) UIButton* btnTextFieldContainer;
#property (retain, nonatomic) UITextField* textField;
#property (retain, nonatomic) NSString* text;
-(id)initWithPosition:(CGPoint)position;
#end
BSCustomTextField.m
#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>
#implementation BSCustomTextField
#synthesize btnTextFieldContainer, textField, text;
-(id)initWithPosition:(CGPoint)position{
if (self == [super init]) {
btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.returnKeyType = UIReturnKeySend;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
[btnTextFieldContainer addSubview:textField];
[self addSubview:btnTextFieldContainer];
}
return self;
}
-(void)dealloc{
[btnTextFieldContainer release];
[textField release];
[super dealloc];
}
#end
So, when using this view in the viewDidLoad of the container view (see code below) the view is properly rendered on the desired position and looks exactly as specified but it does not react on touch events and thus does not become first responder when touched.
Code:
searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder = NSLocalizedString(#"lsUserName", #"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];
Calling [searchTextField.textField becomeFirstResponder] programmatically works properly and makes the keyboard to appear.
But, the interesting part is, if I embed the code of BSCustomTextField inline in my container just like this:
UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.clearButtonMode = UITextFieldViewModeAlways;
searchTextField.returnKeyType = UIReturnKeySend;
searchTextField.keyboardType = UIKeyboardTypeEmailAddress;
searchTextField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
searchTextField.placeholder = NSLocalizedString(#"lsUserName", #"");
[btnTextFieldContainer addSubview:searchTextField];
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer];
[btnTextFieldContainer release];
everything works as expected and the textfield reacts on touches. The type of the searchTextField is properly set in the header file for each case. The only difference is that in the first case I have an additional wrapping UIView on the UIButton and the UITextFiled. I have no idea what to do to make the text field to become first responder on touch.
Thanks a lot in advance,
Roman
Ok, I've got the solution. raaz's point to the UIResponder class tipped me off to the idea that there must be something wrong in the responder chain, thus I did a little research and found this topic: Allowing interaction with a UIView under another UIView in which exact the same problem is discussed.
Here is the new working code for BSCustomTextField.m
#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>
#implementation BSCustomTextField
#synthesize btnTextFieldContainer, textField, text;
-(id)initWithPosition:(CGPoint)position{
if (self == [super init]) {
btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.returnKeyType = UIReturnKeySend;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
[btnTextFieldContainer addSubview:textField];
[self addSubview:btnTextFieldContainer];
}
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// UIView will be "transparent" for touch events if we return NO
return YES;
}
-(void)dealloc{
[btnTextFieldContainer release];
[textField release];
[super dealloc];
}
#end
Since the clickable area fills out the entire BSCustomTextField view we can simply return YES in pointInside:withEvent:
Thank you all for pointing me into the right direction.
Refer UIResponder class
This class has instance method becomeFirstResponder: & isFirstResponder:
Also do not forget to set textfield editing property to YES
Oh, you're completely wrong with doing this way. You should add stretchable image into UIImageView and put it below UITextField.
UIImage *backgroundImage = [[UIImage imageNamed:#"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
Or you can try to disable userInteraction in UIButton.

Action for dynamically created UIButton inside UITextView

I have a scroll view; inside I add one or more text views; to each text view I add a button:
UIScrollView
UITexView
UIButton
UITextView
UIButton
...
This is part of the code:
- (void)viewDidLoad {
...
[self loadUI];
...
}
-(void) loadUI {
UITextView *textView;
...
for (...) {
UIButton *editButton = [[UIButton alloc] initWithFrame:
CGRectMake(self.scrollView.frame.size.width - 230, 0, 50, 20)];
...
[editButton addTarget:self action:#selector(editPressed:)
forControlEvents:UIControlEventTouchUpInside];
...
textView = [[CustomTextView alloc] initWithFrame:
CGRectMake(10, dynamicHeight, self.queuedView.frame.size.width - 100, 500)];
textView.userInteractionEnabled = NO;
...
[textView addSubview:editButton];
[editButton release];
...
[self.scrollView addSubview:textView];
[textView release];
}
}
- (IBAction) editPressed:(id)sender {
...
}
For some reason, the editPressed method is not called when I press the button. Any ideas why? I tried setting the interaction enabled for the text view to YES, but still nothing. I changed form (IBAction) to (void), still nothing.
Thank you,
Mihai
Apparently, textView.userInteractionEnabled = YES; did the trick. I tried this before but it did not work. Maybe I had to do a clean build after the change.
Try adding the button to the rightView of the textfield
dotextField.rightView = editButton instead of [textView addSubview:editButton];