2014-12-02 59 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動作和作用下辭職的鍵盤每個文本框