ios - 当我点击返回键时键盘没有退出 (xcode)

标签 ios

我试图让键盘消失,当我在我的应用程序上按下返回键时,我的代码来自 .m:

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

[textField resignFirstResponder];
return YES;

}

和.h中的代码:

- (BOOL)textFieldShouldReturn:(UITextField* )textField;

但是当我运行应用程序并按下返回键时,没有任何反应。我做错了什么?

我的.h:

#import <UIKit/UIKit.h> 
@interface ChemicalInfoViewController: UIViewController{
IBOutlet UIScrollView *ChemicalInforScroller;

}
@property (strong, nonatomic) IBOutlet UITextField *PPMForChlTextField;
@property (strong, nonatomic) IBOutlet UITextField *GallonsForChlTextField;
@property (strong, nonatomic) IBOutlet UITextField *PPMForAlkTextField;
@property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkTextField;
@property (strong, nonatomic) IBOutlet UITextField *PPMForPHTextField;
@property (strong, nonatomic) IBOutlet UITextField *GallonsForPHTextField;
- (IBAction)SumbitCDI:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UILabel *PPMLabelForChl;
@property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForChl;
@property (strong, nonatomic) IBOutlet UILabel *PPMLabelForAlk;
@property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForAlk;
@property (strong, nonatomic) IBOutlet UILabel *PPMLabelForPH;
@property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForPH;
@property (strong, nonatomic) IBOutlet UITextField *ChlorinePoundsTextField;
@property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkDecreaser;
@property (strong, nonatomic) IBOutlet UITextField *PhPoundsTextField;
@property (strong, nonatomic) IBOutlet UITextField *AlkPoundsTextField;
@property (strong, nonatomic) IBOutlet UITextField *LbsForAlkDecreaser;
@property (strong, nonatomic) IBOutlet UITextField *PPMForAlkDecreaser;
@property (strong, nonatomic) IBOutlet UITextField *LbsForPhDecreaser;
@property (strong, nonatomic) IBOutlet UITextField *PPMForPhDecreaser;
@property (strong, nonatomic) IBOutlet UITextField *GallonsForPhDecreaser;
- (IBAction) clickedBackground;
@interface ChemicalInfoViewController : UIView <UITextField>
@end

还有我的.m:

#import "ChemicalInfoViewController.h"

@interface ChemicalInfoViewController ()

@end

@implementation ChemicalInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}





- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}


- (IBAction) clickedBackground {

[self.view endEditing:YES];

}

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

[textField resignFirstResponder];
return YES;

}

最佳答案

在您的 .h 中,删除:

- (BOOL)textFieldShouldReturn:(UITextField* )textField;

它会正常工作,因为在您的 .h 中,您遵守协议(protocol):

@interface yourViewController : UIView <UITextFieldDelegate>

然后像这样设置委托(delegate):

yourTextField.delegate = self;

关于 @end 的编辑:

在你的.m中,在底部添加一个@end;所以,它变成了(滚动到最底部):

@implementation ChemicalInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}


- (IBAction) clickedBackground {

[self.view endEditing:YES];

}

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

[textField resignFirstResponder];
return YES;

}

@end //Here :-)

关于ios - 当我点击返回键时键盘没有退出 (xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258739/

相关文章:

ios - UILabel 周围的两条水平线

ios - 偏航不断变化的核心运动

c++ - 在 iOS 上使用 OpenCV 文件存储

ios - 'pathExtension' 不可用 : Use pathExtension on URL instead

ios - 根据确切的 URL 隐藏按钮

android - 如何更改 mapview flutter 的颜色应用栏?

ios - 如何在 iOS 中添加静态 JSON? (将字符串转换为 JSON)

ios - 用 cocos2d 3 在圆圈中蒙版 Sprite

ios - AVCaptureSession如何使静音输入音频设备?

ios - Google 移动广告 SDK 在没有 AppMeasurement 的情况下进行了初始化