Don't show the keyboard when I tapped in TextField - ios

I am new to Objective C and iOS development in general. I am trying to create an app that would make an UITextField to tapped. I want the keyboard not to show when I tap on the textfield. then I want many times to tap textfield and don't show the keyboard.
I Want to try this code but it didn't work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

you can called this functions in delegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!textField.inputView) {
//it hides the keyboard, but cursor will show
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}

You can implement delegate method like,
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
//Or just return No
}
Make sure that you have confirm protocol UITextFieldDelegate and you have set delegate property of your textField to self.

Related

When I tap the TextField and don't show the keyboard. How can i use?

I am new iOS Application Developer. and I want to try when i tap the textfield and don't show the keyboard but textfield to tap then show the cursor point will be show. how can i try this?
i want try this code but it did't work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
You can call the Function.
and implement the .h file.
#interface ViewController : UIViewController<UITextFieldDelegate>
implement the .m file.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!textField.inputView) {
//it hides the keyboard, but cursor will show
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
If you don't allow people to tap in TextField, using textfield's delegate method:
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return false;
}

Keyboard for textfield appears after second tap (time to time)

I have one view in my app where there is 1 text field. And I've noticed that keyboard appears after second tap.
But it's interesting that on iPhone it's time to time (some time appears after first tap at once, and some time after second tap only).
On iPad looks like it more ofter appears after second tap only.
I use UITextFieldDelegate
in viewDidLoad I assign the delegate _locationTextField.delegate = self;
and I use delegate methods textFieldDidBeginEditing, textFieldDidEndEditing, textFieldShouldReturn
e.g.:
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_locationNameBeforeManualEdit = _locationTextField.text;
// save the previod city value to compare after did end editing
NSLog(#"textFieldDidBeginEditing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self continueButtonPressed:nil];
// [textField resignFirstResponder];
return YES;
}
in storyboard
What could be the problem?
Found solution here on Stackoverflow - solution related with keyboard preload:
- (void)preloadKeyboard {
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
}
This method should be used in application: didFinishLaunchingWithOptions: method of AppDelegate.

Setting the delegate of a UITextField programatically from a UIView

I have a custom UIView which creates a UITextField as a subview while the application is running. I have been stuck on this for hours now, but cannot figure out how to create delegates to hide the keyboard and determine when the "done" button is pressed. I'm still pretty new to iOS development, so any help here would be greatly appreciated.
Thanks.
u can do this set
yourtextfieldname.delegate=self;
when u hide your keyboard.. then u can call that textfield and do this code...
[txtfieldname resignFirstResponder];
or u can try this.. also
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfieldname resignFirstResponder];
}
or best method
-(BOOL)textfieldshouldreturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
If you are using keyboard done button, set the delegate to the textfield
texfield.delegate = self;
then the below method will be called when done button is clicked
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you are using done button on a toolbar created by you then add a target to the doneButton then call below method in the selector
[textField resignFirstResponder];
This may help you.First use UITextFieldDelegate and set
yourtextfield.delegate=self;
Then use
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
}
// and also you can use this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
return YES;
}

how to erase or clear UITextField

how to clear text field in ios,
How can I make a textfield box remove all content on the users first keypress?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([tfieldDOB.text length] == 4)
{
tfieldDOB.text=[NSString stringWithFormat:#"%#/",tfieldDOB.text];
}
else if([tfieldDOB.text length]==7)
{
tfieldDOB.text=[NSString stringWithFormat:#"%#/",tfieldDOB.text];
}
return YES;
}
change the textfield attribute clear button mode in appears while editing
or other choice just use the single line, where you need to add
yourtextfieldname.text=#""; //it is used for clear the textfield values
Swift
yourtextfieldname.text=""
or another way
clearField =#"YES";
if([clearField isequaltostring:#"YES"]) //check this line in
{
tfieldDOB.text = #"";
clearField =#"NO";
}
Implement the text field's delegate method textFieldShouldBeginEditing: and set the text as empty string when the text field is just about to being editing.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField setText:#""];
return YES;
}
Or you can set the property clearsOnBeginEditing of the textfield as
[textField setClearsOnBeginEditing:YES];
and it will clear the text when editing begins

UitextField shifts control to next textField upon using keyboard input

I am new to iPhone programming. I have two textfields in iPhone with numberPad Keyboard type and i am trying to implement a simple logic that on typing a single digit using numberPadKeyBoard, the control should shift to next textField i.e. second textfield should become FirstResponder. I don't know how to implement this. Please guys any help would be appreciated.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == urFirstTextField) {
[urFirstTextField resignFirstResponder];
[urSecondtextField becomeFirstResponder];
}
}
UPDATE
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField != urFirstTextField)
{
[textField resignFirstResponder];
[urFirstTextField becomeFirstResponder];
return NO;
}
return YES;
}
Suppose you have 2 text field textField1 and textField2 then implement the delgate methods of the UITextFieldDelegate as
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(textField == textField1)
{
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
return YES;
}
go through the delegate method of UITextField named "textField:shouldChangeText:inRange:" ..... check the length of your textField and make second textField as first responder before returning YES;
Or you can play with a lot of other delegate methods defined

Resources