委托UITextField不工作...返回按钮没有响应

Ere*_*rez 13 iphone objective-c ios ios-simulator

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

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

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

我正在使用模拟器,还没有在iphone上测试.(xcode 3.2.5 64位与4.2模拟器).

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Run Code Online (Sandbox Code Playgroud)

在:基本视图控制器连接 - >连接 - >出口,我有:委托 - 文件的所有者.并且在引用出口的文件所有者中有:delegate - 圆形样式文本.....

编辑 - 我以前忘了提,我检查,方法没有被调用!

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

}

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

好的,另一个编辑 - 用我所有的代码:只是无法理解该怎么做.... 这是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
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

也许现在我更清楚了,抱歉我之前没有这样做过.任何人都知道我做错了什么?它应该是相当紧张的前进.....

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

对于每个想要帮助的人来说都是10倍...我非常喜欢这个框架,在c ++和java,python以及其他很多东西之后它真是太棒了...而且我正在使用一本书,但它适用于ios 3.1,也许这就是问题.....

Rog*_*Rog 31

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

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

@interface YourClass : ... <UITextFieldDelegate>

同时为您声明属性和出口UITextField,在IB中进行适当的连接并手动声明selfUITextField委托:

self.yourUITextFieldObject.delegate = self;

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


小智 6

只需在中写一行

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

    NSLog(@"%@",textField.text);
}
Run Code Online (Sandbox Code Playgroud)