ios UITextField 的委托不起作用...返回按钮没有响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4438478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:17:42  来源:igfitidea点击:

Delegate for UITextField not working...Return button not responding

iosiphoneobjective-cios-simulator

提问by Erez

I've just started with xcode and objective-c and did some very basic apps, but what i'm having problem with is very basic this. the keyboard return button not hiding the keyboard.

我刚刚开始使用 xcode 和 Objective-c 并做了一些非常基本的应用程序,但我遇到的问题是非常基本的。键盘返回按钮不隐藏键盘。

I've searched the internet for the solution and all they say is to connect delegate to the file's owner and add the function and it should work, i did that and nothing is working.

我已经在互联网上搜索了解决方案,他们所说的只是将委托连接到文件的所有者并添加该功能,它应该可以工作,我这样做了,但没有任何效果。

I have an ok button and it is working and also clicking on any free space on the screen is working, just the return button....

我有一个确定按钮,它正在工作,并且点击屏幕上的任何可用空间也正在工作,只是返回按钮......

I am using the simulator, not testing on iphone yet. (xcode 3.2.5 64 bit with the 4.2 simulator).

我正在使用模拟器,尚未在 iphone 上进行测试。(带有 4.2 模拟器的 xcode 3.2.5 64 位)。

This is the line of code that should connect the delegate to every textFiled. 1. i've tried already to return both YESand NO, didn't work. 2. i've tried both a specific object name for the textField and this general way, didn't work.

这是应该将委托连接到每个 textFiled 的代码行。1. 我已经尝试过返回YESNO,没有奏效。2. 我已经尝试了 textField 的特定对象名称和这种通用方式,但没有奏效。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

In the: basic view controller connection -> connections -> outlets, i have the: delegate -- File's Owner. and in the file's owner in referencing outlets there is: delegate - Round style text.....

在:基本视图控制器连接 -> 连接 -> 插座中,我有:委托 - 文件的所有者。并且在引用网点的文件所有者中,有:delegate - Round style text .....

EDIT - i forgot to mention before, i've check and the method isn't being called!!!

编辑 - 我之前忘记提到了,我已经检查过并且没有调用该方法!!!

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"Working!!!");
[textField resignFirstResponder];
return YES;

}

}

what should i do to make it happen? that is why people say to connect the delegate, but in my case it is connected and not triggering the function...i know it is kind of dumb question but for a nobie like me the solution is not obvious...

我该怎么做才能让它发生?这就是为什么人们说要连接委托,但在我的情况下它是连接的而不是触发功能......我知道这是一个愚蠢的问题,但对于像我这样的新手来说,解决方案并不明显......

OK, another Edit - with all my code: just can't understand what to do....This is: basicViewController.h:

好的,另一个编辑 - 用我所有的代码:只是不明白该怎么做......这是basicViewController.h::

#import <UIKit/UIKit.h>

@interface basicViewController : <#superclass#> <UITextFieldDelegate>

@interface basicViewController : UIViewController <UITextFieldDelegate> {
//every object that we want to interact with (like text field or lable) is call an   outlet!!!!
//here we define the outlets for our program
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
 }

//here are the getters and setter for our outlets
@property (nonatomic, retain) IBOutlet UITextField *txtName;
@property (nonatomic, retain) IBOutlet UILabel *lblMessage;

//method decleration for the OK button action
- (IBAction) doSomething;



//method for hiding the keyboard when clicking on empty area in the app
   //we will put an invisible button on all area and clicking on it will make keyboard disapear
   - (IBAction) makeKeyboardGoAway;

   @end

This is basicViewController.m:

这是basicViewController.m

 #import "basicViewController.h"

 @implementation basicViewController

 //synthesizeing the objects that we made' this will create the getter and setters automaticly
 @synthesize txtName;
 @synthesize lblMessage;

 - (IBAction) doSomething{
// makeing keyboard disapear when pressing ok button (doing that form the text field)
//when pressing the OK button, the keyboard will disapear and when clicking in the text field it will show again
[txtName resignFirstResponder];



NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@",txtName.text];

//the objective-c way for setting the test in the text field
[lblMessage setText:msg];   
//the regular object oriented way
//lblMessage.text = msg;
[msg  release];
 }    

 - (IBAction) makeKeyboardGoAway{
[txtName resignFirstResponder];
 } 

 //when clicking the return button in the keybaord
 - (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"Working!!!");
[textField resignFirstResponder];
return YES;
 }


 - (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
 }

 - (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
 }


 - (void)dealloc {
     [super dealloc];
 }

 @end

Maybe now i am more clear, sorry i didn't do it before. Any one has an idea what am i doing wrong? it should be pretty strait forward.....

也许现在我更清楚了,抱歉我以前没有这样做。任何人都知道我做错了什么?前进应该很困难......

EDIT - Adding an image of all the elements, i hope that will help to help me :-)alt text

编辑 - 添加所有元素的图像,我希望这会帮助我:-)替代文字

10x a lot for every one that is trying to help....i really like this framework, it is so great after c++ and java, python and many other...and i am working with a book, but it is for ios 3.1, maybe that is the problem.....

对于每个试图提供帮助的人来说都是 10 倍......我真的很喜欢这个框架,它在 c++ 和 java、python 和许多其他之后非常棒......而且我正在写一本书,但它是为 ios 编写的3.1,这也许就是问题.....

回答by Rog

Firstly you should check if textFieldShouldReturn:is actually being called by adding an NSLog statement or breakpoint at the beginning of the method.

首先,您应该textFieldShouldReturn:通过在方法的开头添加 NSLog 语句或断点来检查是否实际被调用。

Once that's out of the way, try an manually declare that your view controller conforms to <UITextFieldDelegate>protocol in your interface file:

完成后,尝试手动声明您的视图控制器符合<UITextFieldDelegate>接口文件中的协议:

@interface YourClass : ... <UITextFieldDelegate>

@interface YourClass : ... <UITextFieldDelegate>

Also declare a property & outlet for your UITextField, make the appropriate connections in IB and manually declare selfas the UITextFielddelegate with:

还为您的 声明一个属性和插座UITextField,在 IB 中建立适当的连接并手动声明selfUITextField委托:

self.yourUITextFieldObject.delegate = self;

self.yourUITextFieldObject.delegate = self;

Once that's done see if your method above is now being called and make sure you return YES.

完成后,查看上面的方法是否正在被调用,并确保返回 YES。

回答by user1229932

Just write one line in the

只写一行

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
}

before return YES; the final version will be as given below:

返回之前是;最终版本如下:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"%@",textField.text);
}

回答by fofo

You need to assign the delegate of the textfields to your file owner. The textfields are sending the message, but doesn't have a delegate to respond to it.

您需要将文本字段的委托分配给您的文件所有者。文本字段正在发送消息,但没有委托来响应它。

Use the interface builder to do that. You have to implement this method..

使用界面构建器来做到这一点。你必须实现这个方法..

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  [textField resignFirstResponder];
  return YES;
}

回答by Jordy

Like Rog said, don't forget to register the textfield to the delegate, you can do this manually as he said but in Storyboard you can just control drag from all of your textfields to the view controller and register the delegate (choose delegate). Only the textfields that are registered can make use of all those methods.

就像 Rog 说的,不要忘记将文本字段注册到委托,您可以按照他所说的手动执行此操作,但在 Storyboard 中,您只需控制从所有文本字段到视图控制器的拖动并注册委托(选择委托)。只有注册的文本字段才能使用所有这些方法。

So this line is important:

所以这一行很重要:

self.yourUITextFieldObject.delegate = self;

Or even more easy these days is to just use the storyboard:

或者现在更简单的是只使用故事板:

enter image description here

在此处输入图片说明

回答by Nareshkumar

put a log at the

把日志放在

- (IBAction) makeKeyboardGoAway

function. I think its this method everytime anything is tapped on the screen. In that case, you will need to send the touch event to the text field. Not sure how this is done but that should do it. Else try removing the which takes care of tap(click) all over the view and try to do what you are doing.

功能。我认为每次在屏幕上点击任何东西时都是这种方法。在这种情况下,您需要将触摸事件发送到文本字段。不确定这是如何完成的,但应该这样做。否则,请尝试删除负责整个视图中的点击(单击)并尝试执行您正在执行的操作。

回答by Sascha

Most likely the problem is that your actual view controller in the running application is not a "basicViewController" but a UIViewController that does not implement the UITextFieldDelegate-protocol.

最有可能的问题是您正在运行的应用程序中的实际视图控制器不是“basicViewController”,而是未实现 UITextFieldDelegate 协议的 UIViewController。

What you've done in the interface builder by selecting your class "basicViewController" as the FilesOwner is just declaringthe FilesOwner-object in your running application to be of type basicViewController; the actual object is notinstantiated by this declaration and in your case it is not in the xib / nib.

您在界面构建器中通过选择类“basicViewController”所做的工作,因为 FilesOwner 只是将正在运行的应用程序中的 FilesOwner 对象声明为 basicViewController 类型;实际对象通过此声明实例化,在您的情况下,它不在 xib / nib 中。

Some other part of your code actually instantiates a view controller object and loads the xib / nib file. At that place, I guess your code is instantiating a UIViewController (typically by auto-generated code) and not an instance of your basicViewController; you simply have to change the class there.

代码的其他部分实际上实例化了一个视图控制器对象并加载了 xib / nib 文件。在那个地方,我猜你的代码正在实例化一个 UIViewController(通常是通过自动生成的代码)而不是你的 basicViewController 的实例;你只需要在那里改变班级。

Furthermore, this error often happens when using a UINavigationController or UITabBarController in the Interface Builder that is (should be) configured to instantiate and load other custom views. If you use such a higher-level controller, double-check that it is actually configured to use your basicViewController, not UIViewController when loading your view from the xib / nib.

此外,在(应该)配置为实例化和加载其他自定义视图的界面构建器中使用 UINavigationController 或 UITabBarController 时,经常会发生此错误。如果您使用这样一个更高级别的控制器,请在从 xib / nib 加载您的视图时仔细检查它是否实际配置为使用您的 basicViewController,而不是 UIViewController。

Hope, that solves the issue!

希望,这解决了问题!

回答by doxsi

textfield is in a subview? in this case, make sure textfield have as delegate the FileOwner.

文本字段在子视图中?在这种情况下,请确保文本字段具有作为 FileOwner 的委托。

回答by Bluely

Can you try this..

你可以试试这个吗..

@interface ClassName : SuperClass < UITextFieldDelegate >

回答by OhadM

You can always dismiss the keyboard when you don't even know which view the text field is in by using:

当您甚至不知道文本字段在哪个视图中时,您始终可以使用以下命令关闭键盘:

Objective-C:

目标-C:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) 
                                           to:nil 
                                         from:nil 
                                     forEvent:nil];

Swift:

迅速:

UIApplication.sharedApplication().sendAction("resignFirstResponder", 
                                           to:nil, 
                                         from:nil, 
                                     forEvent:nil)

回答by kamalesh kumar yadav

Use like this...

像这样使用...

textfield.delegate=self;

and use the UITextFieldDelegatein .h class

并使用UITextFieldDelegatein .h 类