我搜索了互联网上的解决方案,他们所说的就是将委托人连接到文件的所有者,并添加该功能,它应该工作,我做到了,没有任何工作.
我有一个确定按钮,它正在工作,还点击屏幕上的任何可用空间正在工作,只是返回按钮….
我正在使用模拟器,没有在iphone上测试. (xcode 3.2.5 64位与4.2模拟器).
这是应该将代理连接到每个textFiled的代码行.
我已经尝试返回YES和NO,没有工作.
我已经尝试了一个特定的对象名称为textField和这种一般的方式,没有工作.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
在:基本视图控制器连接 – >连接 – >我有:代理 – 文件的所有者.在文件所有者的引用网点有:代表 – 圆形文本…..
编辑 – 我以前忘了提到,我已经检查,方法不被称为!
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"Working!!!");
[textField resignFirstResponder];
return YES;
}
我应该怎么做才能实现呢?这就是为什么人们说连接代表,但在我的情况下,它是连接的,而不是触发功能…我知道这是一个愚蠢的问题,但对于像我这样的新手,解决方案不是很明显…
好的,另一个编辑 – 所有我的代码:只是不明白做什么….
这是: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
这是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
也许现在我更清楚了,对不起,我以前没有这样做.
任何人都有一个想法我做错了什么?应该是漂亮的海峡前进…..
编辑 – 添加所有元素的图像,我希望这将有助于我:-)
对于每一个试图帮助的人来说,10倍以上…我真的很喜欢这个框架,它在c和java,python等许多…之后是如此之大,而且我正在和一本书一起工作,但它是为了ios 3.1,也许这是问题…..
解决方法
一旦遇到这种情况,请尝试手动声明您的视图控制器符合< UITextFieldDelegate>您的接口文件中的协议:
@interface YourClass:…< UITextFieldDelegate>
还声明财产&您的UITextField的插座,在IB中进行适当的连接,并手动将自己声明为UITextField委托:
self.yourUITextFieldobject.delegate = self;
一旦完成,看看上面的方法是否正在被调用,并确保返回YES.