2016-01-21 64 views
3

我無法在輸入文本後讓鍵盤消失。我有Xcode的以前版本的許多解決方案,但沒有對的Xcode 7.我現在ViewController.h文件看起來像:如何在iOS中以編程方式使鍵盤消失

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITextFieldDelegate> 
@property (strong, nonatomic) IBOutlet UITextField *txtUsername; 
@property (strong, nonatomic) IBOutlet UITextField *txtPassword; 

@end 

我.m文件看起來像:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize txtUsername; 
@synthesize txtPassword; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.txtUsername.delegate = self; 
    self.txtPassword.delegate = self; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (BOOL) textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

@end 

我已經分配從textField委託給ViewController。

更新我正在使用的視圖未分配給ViewController類,因此沒有繼承我需要的textFielfShouldReturn方法。

+0

有你,如果'testFieldShouldReturn驗證:'方法實際上是被稱爲? – rmaddy

+0

您的IBOutlets是否已連接? – EmilioPelaez

+0

@EmilioPelaez我有插座連接到ViewController – BlackAperture

回答

4

嘗試:

[self.view endEditing:YES]; 
+0

這不應該需要。 OP發佈的代碼應該可以工作。 – rmaddy

+0

爲什麼沒有試圖解決OP的問題獲得如此多的選票? – rmaddy