I've been searching the web for a really long time, and I can't get this to work. On my text field I have, when I click on it and then press done or return it won't go away. I've done all the steps for every single tutorial but I still can't get it to work. I'm on firmware 3.1.2, but anyway here is the code in my ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
blah.delegate = self;
blah.returnKeyType = UIReturnKeyDone;
}
- (BOOL)blahShouldReturn:(UITextField *)blah{
[blah resignFirstResponder];
return YES;
}
viewcontroller.h:
#interface BlahViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *blah;
}
These are just cut outs from the files. Anyway can anyone help me? I can't get rid of the keyboard when I click on it...
Thanks,
Kevin
I'm confused. Are you actually expecting a method called blahShouldReturn: to get called when you press the Return button? If you want to use the textFieldShouldReturn: delegate method, it has to be called textFieldShouldReturn:. You can use the UITextField parameter supplied with that method to determine which text field is sending the message. For example:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == blah) {
[textField resignFirstResponder];
} else if (textField == someOtherTextField) {
// Do something else
}
return YES;
}
.m file:
- (void)viewDidLoad {
[super viewDidLoad];
blah.delegate = self;
blah.returnKeyType = UIReturnKeyDone;
//Call hideKeyboard action when done button is clicked
[blah addTarget:self action:#selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
}
//Custom hide keyboard action
- (void)hideKeyboard {
[blah resignFirstResponder];
}
.h file:
//Dont need delegate for this method of hiding keyboard!
#interface BlahViewController : UIViewController {
IBOutlet UITextField *blah;
}
- (void)hideKeyboard;
#end
Related
I am attempting to implement a delegate method on NSTextField as described in this article from Apple. My goal is for the NSTextField to accept carriage returns and tabs. I have read elsewhere (including the linked article) that NSTextView is a better choice. However, I am working within a multiplatform framework that lacks support for NSTextView, and NSTextField will do the job if I can get it to accept carriage returns.
Based on the article, here is my code:
#interface MyTextFieldSubclass : NSTextField
{}
- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
#end
#implementation MyTextFieldSubclass
- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
{
BOOL result = NO;
if (commandSelector == #selector(insertNewline:))
{
// new line action:
// always insert a line-break character and don’t cause the receiver to end editing
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
else if (commandSelector == #selector(insertTab:))
{
// tab action:
// always insert a tab character and don’t cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
#end
Additionally, in the Identity Inspector of the text field, I have changed the class name from the default NSTextField to my class name. However, when I run my program, the delegate method never gets called. Is there something else I have to do to set this up in Interface Builder?
There are a few parts of the documentation you linked which is pertinent that I think may have been neglected.
I've copied a few of the lines below:
Should you decide to keep using NSTextField, allowing the tab key and/or allowing enter and return keys for line-breaks can be achieved by implementing the following delegate method:
(BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector;
Note: When implementing this delegate method in your own object you should set your object up as the "delegate" for this NSTextField.
I've bolded a few of the callouts which I think might have been missed.
This method is within the NSControlTextEditingDelegate protocol within NSControl.h. As such it should be implemented by a class which implements the NSControlTextEditingDelegate (i.e. NSTextFieldDelegate)
One common way of doing this is to have the ViewController "holding" the NSTextField be the NSTextFieldDelegate.
Here's a very simple example using the sample code from Apple you linked:
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : NSViewController <NSTextFieldDelegate>
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
BOOL result = NO;
if (commandSelector == #selector(insertNewline:))
{
// new line action:
// always insert a line-break character and don’t cause the receiver to end editing
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
else if (commandSelector == #selector(insertTab:))
{
// tab action:
// always insert a tab character and don’t cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
#end
Then set your NSTextField's delegate to the ViewController
No need to add a custom subclass.
Alternatively you could probably make the custom text field subclass its own delegate. Something along these lines:
#import "MyTextFieldSubclass.h"
#interface MyTextFieldSubclass() <NSTextFieldDelegate>
#end
#implementation MyTextFieldSubclass
- (instancetype)init {
self = [super init];
if (self) {
self.delegate = self;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.delegate = self;
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.delegate = self;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
BOOL result = NO;
if (commandSelector == #selector(insertNewline:))
{
// new line action:
// always insert a line-break character and don’t cause the receiver to end editing
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
else if (commandSelector == #selector(insertTab:))
{
// tab action:
// always insert a tab character and don’t cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
#end
I have set the image to textFiled using rightmodeview on clicking button,
now what I want is, I want to hide the image on tapping the textField without touching button?
Help me out
Do something like:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
textfield.rightView=nil;
//Do other stuff,if you want to do
}
I created sample one and I tried it works fine.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *txtFldImage;
#end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
txtFldImage.rightViewMode = UITextFieldViewModeAlways;
txtFldImage.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"boss-icon.png"]];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
txtFldImage.rightView = nil;
}
When run the app
When I enter or touch the textField
this code is working for me
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//set right view nil
}
and make sure you set delegate of UITextField for your textfield
Add below code in viewDidLoad
yourTextField.delegate = self;
I'd like to thanks you in advance for your answers. I read all stuff about this but I couldn't solve it.
I know how to hide keyboard on UITextField, just like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[placename resignFirstResponder];
[address resignFirstResponder];
return YES;
}
But I also have an UITextView and I don't know how to hide it. I don't want to hide it with a button I want to hide it with a "Done" in the return.
I read something like this:
[textField setReturnKeyType: UIReturnKeyDone];
But... how and where to implement it? I'm new on this so I need to know it step by step,please.
Thanks so much.
(Sorry I'm not able to send an image...new user ;) )
Its easy. Have textview delegate in your controller and add following method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
There is one disadvantage using this, user can not write multi line statements. To solve this you can have a create view over keyboard and have button over there and add action to resign your textview.
Credit to this question
Additional to #JanakNirmal Answers, the code for the .h and .m. Works fine for me.
.h
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *textField; // connected with IB
#end
.m
#interface ViewController ()
- (void)viewDidLoad
{
[super viewDidLoad];
// Done key and hide keyboard
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
}
return YES;
}
#end
I just can't make the "Done" button to quit the keyboard.
I used this in my controller.h file
- (IBAction)textFieldDoneEditing:(id)sender;
and this for my controller.m file
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponer];
}
and I'm mixed up in wiring the .xib part.
Make the controller a delegate of the UITextField/UITextView in IB or from code like textField.delegate = self;
Editted:
For this you need to declare the controller a delegate of UITextFieldDelegate/UITextViewDelegate as
#interface Controller : <UITextFieldDelegate> { ...
, then override the method:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
for UITextField and
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
for UITextView
In your .xib, right click on your text view, drag to "File's Owner", and click "delegate". Should work now?
Edit: Whoops, sorry I'm an idiot, do what that other guy says. If you don't know how to set the delegate in code though, you can do it my way in IB.
Let me make my first contribution:
If you have multiple text fields, group them in a #property (strong, nonatomic)
*.h
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *collectingData;
*.m
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
for (UITextField *boxes in collectingData) {
[boxes resignFirstResponder];
}
return YES;
}
Hi I find some questions like that but they talk about textView, I have ViewController, with scrollView where are 6 textfield and one textView I want a function which makes the keyboard disappear on on done/return button click.I implemented functions resign to first responder, which hide my keyboard when i click outside of scrollView, but that is not exactly i want, because i like to make it disappear on button click too.
THanks for any help
Set up a class that conforms to the UITextFieldDelegate protocol and make the delegate of your text fields an instance of this class. Implement the method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
As follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Hi i found it out so the point with textfields is to add this lines at viewdidload:
textFieldOne.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
textFieldTwo.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
...
And this implement method :
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textFieldOne) {
[textFieldOne resignFirstResponder];
}
...
}
U can use this method to hide the keyboard by clicking any where in the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.
.h
//
// ViewController.h
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
#property (weak, nonatomic) IBOutlet UITextField *theTextField;
#end
.m
//
// ViewController.m
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// _theTextField is the name of the parameter designated in the .h file.
_theTextField.returnKeyType = UIReturnKeyDone;
[_theTextField setDelegate:self];
}
// This part is more dynamic as it closes any text field when pressing return.
// You might want to control every single text field separately but that isn't
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
#end