UITextView issue with ActionSheetStringPicker Pick

2019-02-28 15:48发布

问题:

Check 3 screenshots here first.

Screenshot 1 : Subject is UITextview field. When I tap on subject text field normal keyboard will come. There is no problem here now.

Screenshot 2 : Priority is UITextfield view. Here I use UIActionsheet Picker view. When I click on Priority textfield picker will appear as shown in screenshot. This is also working fine.

My Problem : When I click Priority textfield directly from Subject textview by scrolling without using done button or next button from keyboard. Then following issue is arriving.

or when I move UITextview to other any other Text Field like in Help Topic Text Field or Source Text Field (In this text field UIActionsheet is used.) same problem is arriving.

See screenshot number 3.

here, keyboard and UIActionsheet both are appearing. Here keyboard is not hiding, it is still appearing. It will not appear here, when I move here UITextView to next Text Field i.e in UIActionsheet picker, it has show Picker VIew only.

What is the problem I am not getting. Anybody please tell me solution for that.

Here is some code for that,

@interface EditDetailTableViewController ()
{
    NSNumber *help_topic_id;
    NSNumber *priority_id;



    NSMutableArray * pri_idArray;
    NSMutableArray * helpTopic_idArray;
}

- (void)helpTopicWasSelected:(NSNumber *)selectedIndex element:(id)element;
- (void)priorityWasSelected:(NSNumber *)selectedIndex element:(id)element

- (void)actionPickerCancelled:(id)sender;
@end

@implementation EditDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    help_topic_id=[[NSNumber alloc]init];
    priority_id=[[NSNumber alloc]init];
}

-(void)removeKeyBoard
{
    [self.subjectTextView resignFirstResponder];
}

- (IBAction)priorityClicked:(id)sender {
    [_priorityTextField resignFirstResponder];

    if (!_priorityArray||![_priorityArray count]) {
        _priorityTextField.text=NSLocalizedString(@"Not Available",nil);
        priority_id=0;

    }else{
        [ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
    }

}

- (IBAction)helpTopicClicked:(id)sender {
    [_helpTopicTextField resignFirstResponder];

    if (!_helptopicsArray||!_helptopicsArray.count) {
        _helpTopicTextField.text=NSLocalizedString(@"Not Available",nil);
        help_topic_id=0;
    }else{
        [ActionSheetStringPicker showPickerWithTitle:@"Select Helptopic" rows:_helptopicsArray initialSelection:0 target:self successAction:@selector(helpTopicWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
    }

}

- (void)priorityWasSelected:(NSNumber *)selectedIndex element:(id)element {
    priority_id=(pri_idArray)[(NSUInteger) [selectedIndex intValue]];

    //self.selectedIndex = [selectedIndex intValue];

    //may have originated from textField or barButtonItem, use an IBOutlet instead of element
    self.priorityTextField.text = (_priorityArray)[(NSUInteger) [selectedIndex intValue]];
}

- (void)helpTopicWasSelected:(NSNumber *)selectedIndex element:(id)element {
    help_topic_id=(helpTopic_idArray)[(NSUInteger) [selectedIndex intValue]];
    // self.selectedIndex = [selectedIndex intValue];

    //may have originated from textField or barButtonItem, use an IBOutlet instead of element
    self.helpTopicTextField.text = (_helptopicsArray)[(NSUInteger) [selectedIndex intValue]];
}


#pragma mark - UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {

if (textField.tag==1) {

        [_priorityTextField resignFirstResponder];
         _priorityTextField.tintColor = [UIColor clearColor];

        if (!_priorityArray||![_priorityArray count]) {
            _priorityTextField.text=NSLocalizedString(@"Not Available",nil);
            priority_id=0;

        }else{

            [ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:self.view];
        }

        // return NO;
    }else if(textField.tag==2){
        //[_subjectTextField resignFirstResponder];
        [_helpTopicTextField resignFirstResponder];
         _helpTopicTextField.tintColor = [UIColor clearColor];

        if (!_helptopicsArray||!_helptopicsArray.count) {
            _helpTopicTextField.text=NSLocalizedString(@"Not Available",nil);
            help_topic_id=0;
        }else{
            [ActionSheetStringPicker showPickerWithTitle:@"Select Helptopic" rows:_helptopicsArray initialSelection:0 target:self successAction:@selector(helpTopicWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:self.view];
        }
        // return NO;
    }else{

    }
    // return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    //[textView resignFirstResponder];

    if(textView == _subjectTextView)
    {

        if([text isEqualToString:@" "])
        {
            if(!textView.text.length)
            {
                return NO;
            }
        }

        if([textView.text stringByReplacingCharactersInRange:range withString:text].length < textView.text.length)
        {

            return  YES;
        }

        if([textView.text stringByReplacingCharactersInRange:range withString:text].length >100)
        {
            return NO;
        }

        NSCharacterSet *set=[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "];


        if([text rangeOfCharacterFromSet:set].location == NSNotFound)
        {
            return NO;
        }
    }


    return YES;
}




@end

回答1:

I think the following IBAction code:

- (IBAction)staffClicked:(id)sender {
    [self.view endEditing:YES];
    if (!_staffArray||!_staffArray.count) {
        _assignTextField.text=NSLocalizedString(@"Not Available",nil);
        staff_id=0;
    }else{

        [ActionSheetStringPicker showPickerWithTitle:NSLocalizedString(@"Select Assignee",nil) rows:_staffArray initialSelection:0 target:self successAction:@selector(staffWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
    }
}

... and the following methods:

- (void)staffWasSelected:(NSNumber *)selectedIndex element:(id)element
{
    staff_id=(staff_idArray)[(NSUInteger) [selectedIndex intValue]];
    self.assignTextField.text = (_staffArray)[(NSUInteger) [selectedIndex intValue]];
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}

... will solve your problem for you.



回答2:

It's because of timing issue Action picker will take some time to reload picker on keyboard

BTW you should change your code here And also implement same in textFieldDidBeginEditing

- (IBAction)priorityClicked:(id)sender {
    [_priorityTextField resignFirstResponder];

    if (!_priorityArray||![_priorityArray count]) {
        _priorityTextField.text=NSLocalizedString(@"Not Available",nil);
        priority_id=0;

    }else{
        [ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
    }

}

In the else part you should call showPickerWithTitle method after some delay like below

else{
  [self performSelector:@selector(openPickerWithDelay:) withObject:sender afterDelay:0.5]; // Change 0.5 to whatever you want.

}

Add new method

-(void) openPickerWithDelay:(id) sender {
        [ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];

}

Otherwise you can delay by

double delayInSeconds = 0.5; // Change 0.5 to whatever you want.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

// Do your stuff here

});