Reputation: 111
In my program I need to show text from textfield, which is not user interactive in toolbar, which is accessory view for keyboard. This part works when I press button. Then if device rotates I need to hide keyboard. This part also works fine. But then when I come back to portret and press button which needs to show keyboard nothing heppens.
Here is my code:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController{
UITextField *invisibleTextfield;
UITextField *fieldToEnterText;
}
- (IBAction)buttonAction:(id)sender {
[invisibleTextfield becomeFirstResponder];
}
#pragma mark - Text Field
- (UIToolbar *)keyboardToolBar {
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setOpaque:NO];
[toolbar setTranslucent:YES];
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:fieldToEnterText];
UIBarButtonItem *fixItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
NSArray *itemsArray = @[fixItem,textFieldItem,fixItem];
[toolbar setItems:itemsArray];
return toolbar;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if ([invisibleTextfield isFirstResponder]) {
NSLog(@"did began editing invisible");
[fieldToEnterText becomeFirstResponder];
fieldToEnterText.text = _textField.text;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
_textField.text = fieldToEnterText.text;
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([fieldToEnterText isFirstResponder]) {
[fieldToEnterText resignFirstResponder];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
fieldToEnterText = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-30, 30)];
fieldToEnterText.borderStyle = UITextBorderStyleRoundedRect;
fieldToEnterText.delegate = self;
[fieldToEnterText setKeyboardType:UIKeyboardTypeNumberPad];
[fieldToEnterText setTextAlignment:NSTextAlignmentCenter];
invisibleTextfield = [[UITextField alloc] init];
[invisibleTextfield setKeyboardType:UIKeyboardTypeNumberPad];
invisibleTextfield.inputAccessoryView = [self keyboardToolBar];
invisibleTextfield.delegate = self;
[self.view addSubview:invisibleTextfield];
_textField.text = @"Text";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Upvotes: 4
Views: 5088
Reputation: 23651
By default the iOS Simulator has the hardware keyboard connected. This behavior is similar to how a device behaves when a bluetooth keyboard is connected.
You can tap cmd-k to toggle the software keyboard on/off just like you can use the eject button on a bluetooth keyboard.
If you want to test the disconnected configuration, you can disconnect the hardware keyboard with shift-cmd-k
Upvotes: 9
Reputation: 781
As of iOS8, calling becomeFirstResponder on a UITextView doesn't show the keyboard for me on the iOS simulator, but it does on the real device. So be sure to check it with an actual device.
Upvotes: 0
Reputation: 1582
Looks like when you hit the button you call
[invisibleTextfield becomeFirstResponder];
but when you rotate you call [fieldToEnterText resignFirstResponder];
.
Is this on purpose? Why don't you resign the same text field?
UPDATE:
Why add the text field to the accessory view? I would create a text view below the screen's bottom and set it to firstresponder upon button click. Then catch the KB sliding-up event and slide the text view with it.
Register to these events:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillHideNotification
object:nil];
Look here for values you can get from this notification: UIKeyboardWillShowNotification
Upvotes: 0