Keyboard does not disappear when tapping UIPickerView - objective-c

I have TableView. In each row i have textfield, three textfield have UIPickerView and two are editable, issue is when i tap on picker's textfield after tapping on editable textfield keyboard dosn't disappear here is the code
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
tf_Selected = (UITextField*)textField;
if ([textField.placeholder isEqualToString:#"Work Order "]) {
if (dicWorkOrderNoData.count > 0)
{
[ActionSheetStringPicker showPickerWithTitle:#"Select Work Order" rows:[dicWorkOrderNoData allValues] initialSelection:0 target:self successAction:#selector(selectPicker_ActionHandler:) cancelAction:#selector(cancelSheet_ActionHandler) origin:textField];
}
else if (textField.tag == DateField_UptoCurrentDate)
{
[ActionSheetDatePicker showPickerWithTitle:#"Select Date" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] maximumDate:[NSDate date] target:self action:#selector(selectDate_ActionHandler:) cancel:#selector(cancelSheet_ActionHandler) origin:textField];
return NO;
}
else
{
return YES;
}
}

Try using [textField resignFirstResponder];.
More on that here.

It would better to assign to UITextField's inputAccessoryView Toolbar with [back, next, done] buttons, because you can't be sure that user actually selected pickerview's item or just scrolling them.
If you have few optionns, than use UIActionSheet instead.

Related

Switch from a UIPickerView to a Keyboard

I have a UIPickerView set up with an attached toolbar button that should allow the user to switch from a picker view to a keyboard view. The pickerView should slide down and a keyboard should slide up in it's place... Theoretically.
The pickerView appears when a user clicks the text field in textFieldDidBeginEditing.
elementPicker = [[UIPickerView alloc] init];
[elementPicker setDelegate:self];
elementPicker.showsSelectionIndicator = YES;
...
[_itemElementField setInputView:elementPicker];
When the user clicks the "Use Keyboard" button, I have a method to dismiss the picker and call the keyboard. I can dismiss the pickerview without a problem but CANNOT DISPLAY THE KEYBOARD!!
HELP!
Here is the method called when the user wants the keyboard:
-(void)useKeyboardClicked:(id)sender {
NSLog(#"'USE KEYBOARD' BUTTON CLICKED");
// DISMISS THE PICKER VIEW
[elementPicker removeFromSuperview];
[_itemElementField resignFirstResponder];
_itemElementField.inputAccessoryView = nil;
// SET ELEMENTPICKER TO THE DEFAULT KEYBOARD
elementPicker = UIKeyboardTypeDefault;
[_itemElementField setInputView:elementPicker];
// SHOW KEYBOARD
[_itemElementField becomeFirstResponder];
}
I'm grasping at straws now and need some help! I've even gone so far as to try to define a keyboard in the header file with
#property (strong, nonatomic) IBOutlet UIKeyboard *elementKeyboard;
but that doesn't work AT ALL.
for those that might have this issue in the future, the answer ends up being quite simple.
You need to reload the input view after resetting it to a keyboard with reloadInputViews.
Here's the code:
-(void)useKeyboardClicked:(UITextField *)sender {
NSLog(#"'USE KEYBOARD' BUTTON CLICKED");
[_itemElementField setInputView:UIKeyboardTypeDefault];
[_itemElementField becomeFirstResponder];
[_itemElementField reloadInputViews];
}
Here Is Solution Firstly you have to declare boolean variable checkFlag to keep record of useKeyboardClicked:
then
-(void)useKeyboardClick:(id)sender
{
if (checkFlag) {
checkFlag=false;
[txtField setInputView:nil];
[txtField reloadInputViews];
[txtField setKeyboardAppearance:UIKeyboardAppearanceDefault]; //you can set UIKeyboardAppearnce as Dark,light, alert instead of Default
[txtField setKeyboardType:UIKeyboardTypeDefault]; // you set decimal keyboard
} else {
checkFlag=TRUE;
[txtField setInputView:nil];
[txtField setInputAccessoryView:numberToolbar];
[txtField setInputView:pickerView];
}
}
One thing i m not total wrong but i can't set back picker view instead keyboard ..... try this i gave you hint ...

How do I get a UITextField to accept focus without showing the keyboard?

I am trying to find a way to prevent the keyboard from appearing when the user taps on a TextField but could`t find a way to do it.
I tried this code after I linked my textField to delegate and still it did not work for me, the code was logging but the keyboard did appear.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"BeginEditing");
return YES;
[textField resignFirstResponder];
}
When I return with NO I lose the focus from the textField, which I need.
The textFields I have are filled with values from a buttons on the same view, thats why I don't want the keyboard to appear, and at the same time I want the user to select the textField they want to fill.
if you just want user to select the textfield to fill and does not want to keyboard to show up then you can do the following:
add tag to your textfields
change the code to this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
selectedTextFieldTag = textField.tag;
return NO;
}
use selectedTextField value to identify which textfield to fill in your code. return NO will not allow keyboard to appear.
This will help you for sure.
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
activeField.inputView = dummyView; // Hide keyboard, but show blinking cursor
return YES;
}
I tested and it is working for me. Hope this will be useful for others who have similar issue.
[textField resignFirstResponder]; will not be called because you are returning from the method before it can get called. Does that not fire a warning?
Try returning NO here or if that doesn't work, try disabling user-interaction on the text field:
[myTextField setUserInteractionEnabled:NO];
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"BeginEditing");
[textField resignFirstResponder];
return YES;
}
so here you just use flag int variable to assign the value to focused textfield
define int i; flag globally in .h or .m file
after that in textField Delegate method use bellow code...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == yourtextField1 ) {
i=1;
}
else if (textField == yourtextField2 ) {
i=2;
}
else if (textField == yourtextField3 ) {
i=3;
}
else if (textField == yourtextField4 ) {
i=4;
}
return NO;
}
-(IBAction)yourbutton1_Clicked:(id)sender{
if( i == 1){
yourtextField1.text=yourbutton1.titleLabel.text;
}
else if ( i == 2){
yourtextField2.text=yourbutton1.titleLabel.text;
}
else if ( i == 3){
yourtextField3.text=yourbutton1.titleLabel.text;
}
else if ( i == 4){
yourtextField4.text=yourbutton1.titleLabel.text;
}
else{
NSLog(#"Please Click On TextField");//here you can put AlertView Message
}
}
and so on.......
also you can use common method with sender id of button and also tag......

how to set background image dynamically for dynamically created button using tag?

I have created a row of numberButtons inside a view dynamically.I am getting button highlighted when clicking any number.If I am clicking more than 1 in that row ,all of the clicked buttons get highlighted.What to do for avoiding multiple highlation?
I have used the code as follows
-(void)pressed:(id)sender{
UIButton *button = (UIButton *)sender;
if(!button.selected){
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(highlightButton:) userInfo:button repeats:NO];
} else {
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(unhighlightButton:) userInfo:button repeats:NO];
}
-(void)highlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
button.highlighted = YES;
button.selected = YES;
}
-(void)unhighlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
button.highlighted = NO;
button.selected = NO;
}
I'm assuming that you mean that every button you tap is highlighted without removing the previous highlight.
To only have one button highlighted at a time. Keep track of what button was highlighted and remove its highlight when tapping another button.
- (void)buttonTapped:(UIButton *)button {
if (button != [self lastSelectedButton]) { // don't re-highlight the same button
// remove the highlight of "lastSelectedButton"
[self setLastSelectedButton:button];
// add the highlight to "lastSelectedButton" (not updated to the new button)
}
// Do the rest of you button logic here ...
}
Override your select method by calling deselect method in the end.
So, when you click your control will be selected and deselected instantly.

How to control UIButton on View without IB

I put some UIButtons on UIScrollView without IB.
If I click one button, other buttons are turned off except selected button.
But, When I click another button, other button ( except selected button) are turned off.
I can't confirm count of UIButtons on UIScrollView.
Because count of UIButtons are changed dynamically.
Without IB, How can I control UIButton on UIScrollView?
Plese tell me your advice. Thanks!
you can use:
NSUInteger count = 0;
for(id button in [scrollView subViews])
{
if([button isKindOfClass[UIButton class]])
{
count++;
}
}
NSLog("total buttons: %d",count);
You can tag the UIButtons when creating them [myButton setTag:MY_BUTTON_1] and later use this information when the button is tapped
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
if ([button tag] == MY_BUTTON_1) { /* do something with this button */ }
else if ([button tag] == MY_BUTTON_2) { /* do something differently */ }
else return;
}

Problem with dismissing the keyboard when focus leaves a UITextView

I have 3 uitextfield in my project
I need to when tap inside one of them (uitextfield2 ) a custom subview appear , and need the key board to appear when tap on one another (uitextfield1 )
the problem is when I tab on uitextfield1 , the keypad appear and not go even I clicked return or tap on another uitextfield2
I need the keyboard to disappear when I click out of the uitextfield1 or when click return
I use the following code
- (BOOL)textFieldShouldReturn:(UITextField *)textField { // When the return button is pressed on a textField.
[textField resignFirstResponder]; // Remove the keyboard from the view.
return YES; // Set the BOOL to YES.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// [textField resignFirstResponder];
[self SelectModalityClick]; // will be called If i tapped inside the uitextfield2 to display the custom view
return NO;
}
When you create your UITextField instances, you can set the inputView to whatever UIView subclass you want with something like this.
UITextField *aTextField = [[UITextField alloc] initWithFrame:aRect];
aTextField.inputView = myCustomInputView; // Set this up in your viewDidLoad method
aTextField.delegate = self;
aTextField.tag = MyCustomInputView; // #define this as some integer
[self.view addSubview];
[aTextField release];
You shouldn't need to do anything special to make the correct input view appear, if you have a UITextField instance variable for the first responder, just assign it in textFieldShouldBeginEditing: and resign its first responder status in textFieldShouldReturn:.