Reputation: 101
I can't get my keyboard to go away when I press the return key in iOS. I have in my header file, I am setting self.textField.delegate = self; and I have implemented the following:
-(BOOL)textFieldShouldReturn:UITextField *)textField {
[textField resignFirstResponder];
}
At first it wouldn't do anything when I pressed enter, but now, after connecting the Did End on Exit to the View Controller with the Connections Inspector I am getting an NSInvalidArgumentException reason -[ViewController dismissKeyboard:]: unrecognized selector sent to instance 0x155e10620.
Any ideas? I am using Xcode 6.1. I connected the Did End On Exit to the View Controller in which the textField lies and when I released the connection a small button appeared that said originText on it and I selected that since it was the only option.
Here is my code (abbreviated) -
#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
Upvotes: 0
Views: 194
Reputation: 1448
Remove both the declarations of textField from your .h file, they might be confusing the compiler and overwriting the delegate methods. Also, get rid of your Did End on Exit connection as it is unnecessary for managing the keyboard.
Upvotes: 0
Reputation: 150
You should declare UITextFieldDelegate
in your .h file and then in .m file, use self.textField.delegate = self
.
Upvotes: 0
Reputation: 13323
Try:
[self.view endEditing:YES];
Sometimes that works better as it doesn't matter which control is currently the first responder.
Upvotes: 0
Reputation: 26
-(BOOL)textFieldShouldReturn:UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Upvotes: 0
Reputation: 40030
You have a typo in textFieldSouldReturn
. Thus, this delegate method was never invoked.
Upvotes: 1