2014-12-02 55 views
3

嘿家伙所以即时通讯不知道我在做什么错,但即时通讯尝试让键盘走开,当我按下我的应用程序上的返回键继承我的代码该.M:键盘不辞职当我点击返回键(xcode)

- (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; 

} 

回答

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

这种方式仍然没有工作谢谢你呢,还有其他想法吗? – Jake 2014-12-02 21:04:22

+0

@Jake我刚测试过它。它为我工作。 yourTextField是_你的文本field_;如果你有一个叫做“textField”的textField,那就用它替换它。 – Unheilig 2014-12-02 21:05:08

+0

必须是我的配置有问题,即时通讯不知道,但我还应该检查什么? – Jake 2014-12-02 21:06:57

0

您没有设置代表为您文本框。

在 “viewDidLoad中” 添加

_ChlorinePoundsTextField.delegate =自我;

_GallonsForAlkDecreaser.delegate = self;

_PhPoundsTextField.delegate = self;

_AlkPoundsTextField.delegate = self;

_LbsForAlkDecreaser.delegate = self;

_PPMForAlkDecreaser.delegate = self;

_LbsForPhDecreaser.delegate = self;

_PPMForPhDecreaser.delegate = self;

_GallonsForPhDecreaser。委托=自我;

0

这些方法都似乎是工作由于某种原因,但我就是这样做最终会做是创建一个DidEndOnExit动作和作用下辞职的键盘每个文本框