Reputation: 951
I'm doing a programatic UI for my iOS app, and I have these two UITextField
s. When the client opens the app, they'll select the two fields and give some input, after that, how do I deselect the UITextField
s? Because if you tap or scroll somewhere else on the screen, the keyboard stays on the screen - it doesn't retract like the expected behavior.
So I guess my question is how do you submit a UITextField
without a nib
or storyboard
?
Here's the relevant part of viewDidLoad
, with the two UITextField
s:
UITextField *loginField = [[UITextField alloc] initWithFrame:mistarFrame];
loginField.backgroundColor = [UIColor clearColor];
loginField.textColor = [UIColor whiteColor];
loginField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
loginField.borderStyle = UITextBorderStyleNone;
loginField.autocorrectionType = UITextAutocorrectionTypeNo;
loginField.autocapitalizationType = UITextAutocapitalizationTypeNone;
loginField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
loginField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Student ID" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[header addSubview:loginField];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(mistarFrame.origin.x, (mistarFrame.origin.y + iconHeight), mistarFrame.size.width, mistarFrame.size.height)];
passwordField.backgroundColor = [UIColor clearColor];
passwordField.textColor = [UIColor whiteColor];
passwordField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
passwordField.borderStyle = UITextBorderStyleNone;
passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
passwordField.secureTextEntry = YES;
[header addSubview:passwordField];
Upvotes: 0
Views: 544
Reputation: 349
Also you can conform to UITextFieldDelegate protocol and set
loginField.delegate = self;
passwordField.delegate = self;
and then implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if ([loginField.text isEqualToString:@""] == NO && [passwordField.text isEqualToString:@""] == NO) {
//submit...
}
return YES;
}
That's if you want to submit by pressing return on the keyboard.
Here's a sample implementation file...
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 30, 100, 25)];
[self.view addSubview:tf];
tf.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if ([textField.text isEqualToString:@""] == NO) {
NSLog(@"Submit");
}
return YES;
}
@end
Upvotes: 2
Reputation:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[loginField resignFirstResponder];
//..
}
Try this.
Upvotes: 0
Reputation: 14251
I would suggest adding a UIGestureRecognizer to your view where the UITextViews are located and hook up a "hide keyboard event" to the tap action.
For example, if the UITextViews are in the view of a UIViewController:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *screenTapped = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
screenTapped.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:screenTapped];
}
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
Upvotes: 1