After a some time, searching here for some solutions we found this post:
How to navigate through textfields (Next / Done Buttons)
We tried that but unfortunately it doesn’t work at all. Note that we put the UITextFields inside a UITableView.
We think the problem is around the Delegates but we don’t know how to deal with it. Following i show you our code:
.h:
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
UITableView *loginTableView;
UITextField *textField;
}
@property UITableView *loginTableView;
@property UITextField *textField;
@property UIButton *loginButton;
@property UIButton *cancelButton;
@end
.m:
#import <QuartzCore/QuartzCore.h>
#import "LoginViewController.h"
#import "RootViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
@synthesize loginTableView, textField, loginButton, cancelButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Make rounded corners view
[self.view.layer setCornerRadius:4.0];
[self.view.layer setMasksToBounds:YES];
self.view.layer.opaque = NO;
self.view.backgroundColor = [UIColor whiteColor];
// Create background image view
UIImageView *loginBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height+20)];
// Create background's image
UIImage *backgroundImage = [UIImage imageNamed:@"loginbackground.png"];
loginBackgroundImageView.image = backgroundImage;
[self.view addSubview:loginBackgroundImageView];
// Create logo's image
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
// Create logo's image view
UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - logoImage.size.width/2, self.view.bounds.size.height/9, logoImage.size.width, logoImage.size.height)];
// Set image to logo's image view
logoImageView.image = logoImage;
[self.view addSubview:logoImageView];
// Create login table view
loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - (self.view.bounds.size.width/1.2)/2, self.view.bounds.size.height/3, self.view.bounds.size.width/1.25, 100) style:UITableViewStylePlain
];
loginTableView.delegate = self;
loginTableView.dataSource = self;
// Create login container table view
UIImage *containerImage = [UIImage imageNamed:@"loginform.png"];
UIImageView *containerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - containerImage.size.width/2, self.view.bounds.size.height/3, containerImage.size.width, containerImage.size.height)];
containerImageView.image = containerImage;
[self.view addSubview:containerImageView];
// Custom table view
loginTableView.backgroundColor = [UIColor clearColor];
loginTableView.separatorColor = [UIColor clearColor];
// Disable scroll
loginTableView.scrollEnabled = NO;
// Add login table view to main view
[self.view addSubview:loginTableView];
// Create buttons images
UIImage *loginImage = [UIImage imageNamed:@"loginbtn2.png"];
UIImage *cancelImage = [UIImage imageNamed:@"cancelbtn.png"];
// Create buttons
loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
// Set buttons' normal state image
[loginButton setImage:loginImage forState:UIControlStateNormal];
[cancelButton setImage:cancelImage forState:UIControlStateNormal];
// Place buttons
loginButton.frame= CGRectMake((self.view.bounds.size.width/2)-(loginImage.size.width/2), self.view.bounds.size.height/1.65, loginImage.size.width, loginImage.size.height);
cancelButton.frame= CGRectMake((self.view.bounds.size.width/2)-(cancelImage.size.width/2), self.view.bounds.size.height/1.375, cancelImage.size.width, cancelImage.size.height);
// Set buttons' action
[loginButton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Set button to the main view
[self.view addSubview:loginButton];
[self.view addSubview:cancelButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([indexPath section] == 0) {
textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 200, 25)];
textField.adjustsFontSizeToFitWidth = NO;
textField.textColor = [UIColor darkGrayColor];
textField.backgroundColor = [UIColor clearColor];
if ([indexPath row] == 0) {
textField.placeholder = @"Username";
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyNext;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.tag = 0;
}
else {
textField.placeholder = @"Password";
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyDone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.secureTextEntry = YES;
textField.tag = 1;
}
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.delegate = self;
[textField setEnabled: YES];
[cell addSubview:textField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.imageView.image = [UIImage imageNamed:@"usernameico.png"];
}
else {
cell.imageView.image = [UIImage imageNamed:@"passwordico.png"];
}
}
else { // Login button section
cell.textLabel.text = @"Log in";
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
-(BOOL)textFieldShouldReturn:(UITextField*) formTextField {
NSInteger nextTag = formTextField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[formTextField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
- (void)loginButtonPressed: (UIButton *) sender {
}
- (void)cancelButtonPressed: (UIButton *) sender {
}
@end
Thank you in advance!
The problem is the table. When you ask you text view for it’s superview, it will return the cell, which doesn’t contain any other siblings. Try: