当我按下iOS中的返回键时,我无法让键盘消失。我在我的头文件中,我设置self.textField.delegate = self;我实施了以下内容:
-(BOOL)textFieldShouldReturn:UITextField *)textField {
[textField resignFirstResponder];
}
首先,当我按下Enter键时它不会做任何事情,但现在,在使用Connections Inspector将Exit End on Exit连接到View Controller后,我得到一个NSInvalidArgumentException原因 - [ViewController dismissKeyboard:]: 无法识别的选择器发送到实例0x155e10620。
有什么想法吗? 我正在使用Xcode 6.1。我将Did End On Exit连接到textField所在的View Controller,当我发布连接时,出现一个小按钮,上面写着originText,我选择了它,因为它是唯一的选项。
这是我的代码(缩写) -
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate, UITextFieldDelegate>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *discoveredPerepheral;
@property (strong) CBUUID *myServiceUUID;
@property (strong, nonatomic) NSMutableData *data;
@property (strong, nonatomic) IBOutlet UITextField *originText;
@property (strong, nonatomic) IBOutlet UITextField *destText;
@property (strong, nonatomic) AVSpeechSynthesizer *synth;
@property (strong, nonatomic) AVSpeechUtterance *utter;
@property (weak, nonatomic) IBOutlet UILabel *charLabel;
@property (weak, nonatomic) IBOutlet UILabel *isConnected;
@property (weak, nonatomic) IBOutlet UILabel *myPeripherals;
@property (weak, nonatomic) IBOutlet UILabel *aLabel;
- (IBAction)originText:(id)sender;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
@end
#import "ViewController.h"
@interface ViewController ()
@property int myInt;
@end
@implementation ViewController
bool isConnected = NO;
CBCharacteristic *originChar;
CBUUID *originUUID;
NSString *const tempUUID = @"0499F5DB-DDC6-4BEF-B551-EB69F9254BB9";
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];
_data = [[NSMutableData alloc]init];
originUUID = [CBUUID UUIDWithString:@"0499F5DB-DDC6-4BEF-B551-EB69F9254BB9"];
_myServiceUUID = [CBUUID UUIDWithString:@"16055ED2-606D-47D4-B4C0-8F6BFC903DAB"];
_synth = [[AVSpeechSynthesizer alloc] init];
[_originText setDelegate:self];
[_destText setDelegate:self];
_originText.returnKeyType = UIReturnKeyGo;
} ...
- (IBAction)originText:(id)sender {
if (isConnected) {
NSData *dataToWrite = [_originText.text dataUsingEncoding:NSUTF8StringEncoding];
//[_discoveredPerepheral writeValue:dataToWrite forCharacteristic:originChar type:CBCharacteristicWriteWithResponse];
NSLog(@"Data Written.");
}
NSLog(@"Disconnected.");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"HEy Hey");
[textField resignFirstResponder];
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
@end
答案 0 :(得分:1)
textFieldSouldReturn
中有拼写错误。因此,从未调用此委托方法。
答案 1 :(得分:0)
-(BOOL)textFieldShouldReturn:UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
答案 2 :(得分:0)
尝试:
[self.view endEditing:YES];
有时候效果会更好,因为哪个控件目前是第一响应者并不重要。
答案 3 :(得分:0)
您应该在.h文件中声明UITextFieldDelegate
,然后在.m文件中声明,使用self.textField.delegate = self
。
答案 4 :(得分:0)
从.h文件中删除textField的声明,它们可能会混淆编译器并覆盖委托方法。此外,摆脱退出连接上的Did End,因为管理键盘是不必要的。