当我点击返回键(xcode)时键盘没有辞职

时间:2014-12-02 20:53:18

标签: 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;

}

3 个答案:

答案 0 :(得分:1)

.h中,删除:

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

它会按预期工作,因为在你的.h中,你符合协议:

@interface yourViewController : UIView <UITextFieldDelegate>

并像这样设置代表:

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 :-)

答案 1 :(得分:0)

您没有为文本字段设置代理。

在“viewDidLoad”中添加

  

_ChlorinePoundsTextField.delegate = self;

     

_GallonsForAlkDecreaser.delegate = self;

     

_PhPoundsTextField.delegate = self;

     

_AlkPoundsTextField.delegate = self;

     

_LbsForAlkDecreaser.delegate = self;

     

_PPMForAlkDecreaser.delegate = self;

     

_LbsForPhDecreaser.delegate = self;

     

_PPMForPhDecreaser.delegate = self;

     

_GallonsForPhDecreaser.delegate = self;

答案 2 :(得分:0)

这些方法似乎都没有出于某些原因而起作用,但我最终做的是创建一个DidEndOnExit操作并在该操作下为每个文本域重新签名键盘