I'm not sure it can be able to add DatePicker dynamically under UITextField when tapped on. If possible, help me to show sample code for that.
on click event set on text field
- (void)viewDidLoad {
[super viewDidLoad];
[textField addTarget:self action:#selector(clicked:) forControlEvents:UIControlEventEditingDidBegin];
}
set text field edition false
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
Method when clickd on text filed
-(IBAction)clicked:(id)sender
{
[sender resignFirstResponder];
picker1 = [[UIDatePicker alloc] initWithFrame:CGRectMake(textField.frame.origin.x-40, textField.frame.origin.y+textField.frame.size.height, textField.frame.size.width, 200)];
[picker1 setDatePickerMode:UIDatePickerModeDate];
picker1.backgroundColor = [UIColor whiteColor];
[picker1 addTarget:self action:#selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:picker1];
}
when date value changed this method is called
-(IBAction)startDateSelected:(id)sender
{
NSLog(#"%#",picker1.date);
[textField setText:[NSString stringWithFormat:#"%#",picker1.date
]];
}
I hope it will help you! :)
Related
So I have a UIswitch (firstSwitch) which when it is ON calls an action where it creates another UISwitch (secondSwitch). Then I want to repeat this step where the newly created UISwitch (secondSwitch) calls an action where it creates a new UISwitch (thirdSwitch) when it is switched ON. But the problem is, the first newly created switch (secondSwitch) does not detect the ON state so it cannot create the new UISwitch (thirdSwitch). Any advice or guidance on this would be greatly appreciated. Thank you in advance.
Below are snippets of code to get a better idea (please disregard the positioning of x, y, width, height):
- (void)viewDidLoad {
[super viewDidLoad];
[self loadFirstSwitch];
[firstSwitch addTarget:self action:#selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
//This secondSwitch does not detect the change in state to ON
[secondSwitch addTarget:self action:#selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)loadFirstSwitch {
firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth - 50 - buttonPadding, 527.5 + buttonWidth, 50, 27)];
[scrollView addSubview:firstSwitch];
}
- (void)loadSecondSwitch{
secondSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth - 50 - buttonPadding, 527.5 + buttonWidth, 50, 27)];
[scrollView addSubview:secondSwitch];
}
- (void)loadThirdSwitch{
thirdSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth - 50 - buttonPadding, 527.5 + buttonWidth, 50, 27)];
[scrollView addSubview:thirdSwitch];
}
- (void) switchIsChanged:(UISwitch *)paramSender{
if(paramSender == firstSwitch){
if([paramSender isOn]){
[self loadSecondSwitch];
}else{
NSLog(#"Switch is off");
}
}
if(paramSender == secondSwitch){
if([paramSender isOn]){
[self loadThirdSwitch];
}else{
NSLog(#"Switch is off");
}
}
}
You are adding target to secondSwitch before it is created, it should be done after it is created, better to put in the function loadSecondSwitch, so you code looks like
- (void)loadSecondSwitch{
secondSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth - 50 - buttonPadding, 527.5 + buttonWidth, 50, 27)];
[scrollView addSubview:secondSwitch];
[secondSwitch addTarget:self action:#selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
}
Remove [secondSwitch addTarget:self action:#selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged]; from viewDidLoad.
I hope it works for you.
Cheers.
You're adding a target and selector to the second switch before it's instantiated. Try moving [secondSwitch addTarget:self action:#selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged]; to inside loadSecondSwitch.
I have searched the entire internet and tried different ways to implement UITextFieldShouldReturn, but when I run it in the simulator it just doesn't work.
What I'm trying to do is move from the first textfield(emailTextField) to the second one( nameTextField ) when user clicks next button on the keyboard. The same for the second textfield's keyboard and dismiss the keyboard when user clicks done button on the last textfield( numberTextField ).
Here is my code, can anyone help me with this?
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.emailTextField.placeholder = #"Your Pitt Email(optional)";
self.nameTextField.placeholder = #"Lost ID Name";
self.numberTextField.placeholder = #"Lost ID Series Number(optional)";
[self.emailTextField.delegate self];
[self.nameTextField.delegate self];
[self.numberTextField.delegate self];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//release delegate
-(void)dealloc{
self.emailTextField.delegate = nil;
self.nameTextField.delegate = nil;
self.numberTextField.delegate = nil;
}
//dismiss keyboard when it's called
-(void)dismissKeyboard{
[self.emailTextField resignFirstResponder];
[self.nameTextField resignFirstResponder];
[self.numberTextField resignFirstResponder];
}
//when a textfield begins editing, this will happen
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.emailTextField){
textField.placeholder = #"";
}else if(textField == self.nameTextField){
textField.placeholder = #"";
}else{
textField.placeholder = #"";
}
}
//when a textfield ends editing, this will happen
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.emailTextField){
textField.placeholder = #"Your Pitt Email(optional)";
[self textFieldShouldReturn:self.emailTextField];
}else if(textField == self.nameTextField){
textField.placeholder = #"Lost ID Name";
[self textFieldShouldReturn:self.nameTextField];
}else{
textField.placeholder = #"Lost ID Series Number(Optional)";
[self textFieldShouldReturn:self.numberTextField];
}
}
//return button set up
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//when confirm is pressed
- (IBAction)confirmButton:(id)sender {
[self dismissKeyboard];
}
#end`
Change
[self.emailTextField.delegate self];
to
self.emailTextField.delegate = self;
Also remove those delegates from dealloc. You can put them from viewDidDisappear
Try this code snippet. Here i am taking only two
UITextFields.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField==_txtEmail)
{
[_txtEmail resignFirstResponder];
[_txtPassword becomeFirstResponder];
}
else if (textField==_txtPassword)
{
[_txtPassword resignFirstResponder];
}
return YES;
}
This function is enough to return keyboard from text fields.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
Your these 3 lines are not working.
[self.emailTextField.delegate self];
[self.nameTextField.delegate self];
[self.numberTextField.delegate self];
If you are using storyboard, connect delegates of the text fields with the view.
You can use this code.It is working for me..
-(void)tool{
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(resignKeyboard)];
[doneButton setTintColor:[UIColor whiteColor]];
UIBarButtonItem *Next = [[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(Nextbutton)];
[Next setTintColor:[UIColor whiteColor]];
NSArray *itemsArray = [NSArray arrayWithObjects:Next,flexButton,doneButton,nil];
[toolbar setItems:itemsArray];
[FirstNameTextField setInputAccessoryView:toolbar];
[LastNameTextfield setInputAccessoryView:toolbar];
[EmailAddressTextField setInputAccessoryView:toolbar];
[PasswordTextfield setInputAccessoryView:toolbar];
[ConfirmPasswordTextfield setInputAccessoryView:toolbar];}
-(void)Nextbutton{
if ([FirstNameTextField isFirstResponder])
{
[LastNameTextfield becomeFirstResponder];
}
else if ([LastNameTextfield isFirstResponder])
{
[EmailAddressTextField becomeFirstResponder];
}
else if ([EmailAddressTextField isFirstResponder])
{
[PasswordTextfield becomeFirstResponder];
}
else if ([PasswordTextfield isFirstResponder])
{
[ConfirmPasswordTextfield becomeFirstResponder];
}
else if ([ConfirmPasswordTextfield isFirstResponder])
{
[ConfirmPasswordTextfield resignFirstResponder];
}}
-(void)resignKeyboard{
[FirstNameTextField resignFirstResponder];
[EmailAddressTextField resignFirstResponder];
[LastNameTextfield resignFirstResponder];
[PasswordTextfield resignFirstResponder];
[ConfirmPasswordTextfield resignFirstResponder];}
First of all you need to set the delegate right. Then You need to give your next textfield the becomeNextResponder call which should be ideally done in the shouldReturn delegate method.
In your viewDidLoad method, paste this.
self.emailTextField.tag = 2000;
self.nameTextField.tag = 3000;
self.numberTextField.tag = 4000;
Now edit your textFieldShouldReturn method like this:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
if(textfield.tag<4000)
{
UITextField *nextTextField = (UITextField *)[self.view viewWithTag:textField.tag+1000];
[nextTextField becomeFirstResponder];
}
return YES;
}
you can always use a library to accomplish what you need to do !!
use this library : tpAvoidKeyboard
Read how to use it and there you go all the next buttons and clicks outside text fields to dismiss the keyboard and the the done button on the last textfield to dismiss the keyboard, all those features are already implemented
I have this code, who capture when the user click in my textfield, and call a function:
-(void)viewDidLoad{
[mytextfield addTarget:self
action:#selector(callvariant:)
forControlEvents:UIControlEventTouchDown];
}
-(void)callvariant:(UITextField *)textField{
NovoProdutoMFViewController *mf = [[NovoProdutoMFViewController alloc] init];
[self.navigationController pushViewController:mf animated:YES];
[mf setBusca:textField.placeholder];
}
The code works perfectly, but recently had the need to put a UIScrollView in my project with this code:
-(void)viewDidLoad{
[mytextfield addTarget:self
action:#selector(callvariant:)
forControlEvents:UIControlEventTouchDown];
scroll.contentSize = scroll.frame.size;
scroll.frame = self.view.frame;
[self.view addSubview:scroll];
}
-(void)callvariant:(UITextField *)textField{
NovoProdutoMFViewController *mf = [[NovoProdutoMFViewController alloc] init];
[self.navigationController pushViewController:mf animated:YES];
[mf setBusca:textField.placeholder];
}
Now Sometimes when the user click on textfield call the function and sometimes not call, and at other times it is necessary to keep the text field clicked down for more than 2 seconds to call the function and has sometimes this method does not work.
I do not know what might be happening, but how can I solve this?
If you want to catch the event of user touch a UITextField, You better use its delegate method.
UITextFieldDelegat has a method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
if you do something here and return NO, you will get the same result.
I would like to intercept a touch on a UITextField, for instance when I first load it I give it a #selector() to load a different method instead of the delegate stuff?
This is my attempt:
descriptionText = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 25.0, infoView.frame.size.width - 20, 100.0)];
descriptionText.backgroundColor = [UIColor whiteColor];
descriptionText.textColor = [UIColor blackColor];
descriptionText.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
descriptionText.userInteractionEnabled = YES;
[descriptionText addTarget:self action:#selector(loadInfoView:) forControlEvents:UIControlEventTouchUpInside];
descriptionText.font = [UIFont fontWithName:#"Helvetica" size:15];
// show view
[infoView addSubview:descriptionText];
However when I debug the method:
- (void)loadInfoView
{
NSLog(#"load other view here");
}
Your problem is in the forControlEvents:: try UIControlEventEditingDidBegin
[descriptionText addTarget:self action:#selector(loadInfoView:) UIControlEventEditingDidBegin];
Your #selector(loadInfoView:) is also wrong if you have - (void)loadInfoView
or #selector(loadInfoView:) and - (void)loadInfoView: (id) sender
or #selector(loadInfoView) and - (void)loadInfoView
However, why you don't use the UITextFieldDelegate?
.m
#interface ViewController () <UITextFieldDelegate>
#property (strong, nonatomic) UITextField *descriptionText;
#end
Remember to:
self.descriptionText.delegate = self;
Then:
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == self.descriptionText)
{
[self loadInfoView];
}
}
Keyboard:
If you don't want to show the keyboard you need to add a [descriptionText resignFirstResponder];
I'm not sure you can get actions to be called on a text field on touch like you want.
If that doesn't work, why don't you attach a tap gesture recognizer to your text field?
I need to use the, Name Phone Keyboard type (UIKeyboardTypeNamePhonePad) but having the number pad appear first when the text field is selected.
I need to input data such as
123 -
657 -
450b -
123a
I always need input a number first, but occasionally either a A / B at the end.
You can try to change keyboard type in function of the size of the string:
Implement delegate UITextFieldDelegate
Then set defaut textfield keyboard;
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField setDelegate:self];
Then in the following method, change the keyboard when text size equals 3:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text] length] > 3)
{
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField reloadInputViews];
}
return YES;
}
Another solution is to add an accessory view button (you can change of keyboard manually):
- (void)viewDidLoad
{
[super viewDidLoad];
textField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:#selector(changeKeyboard) forControlEvents:UIControlEventTouchDown];
textField.inputAccessoryView = button;
}
-(void)changeKeyboard
{
if([textField keyboardType] == UIKeyboardTypeNumberPad)
{
textField.keyboardType = UIKeyboardAppearanceDefault;
}
else
{
textField.keyboardType = UIKeyboardTypeNumberPad;
}
[textField reloadInputViews];
}
click on the textfield,then go to Hide or show utilities ie View(you will find it at right hand side beside the editor.There you select the third view ie the most right one).
Then you click the attributes inspector.In that you will find a menu called "keyboard" which is set as default,but you can select whichever pad you want.
Hope it works for you.