ActionSheetStringPicker Picker 视图的 UITextView 问题
UITextView issue with ActionSheetStringPicker Picker view
先在这里查看3张截图。
屏幕截图 1:主题是 UITextview 字段。当我点击主题文本字段时,将出现正常键盘。现在这里没有问题了。
屏幕截图 2:优先级为 UITextfield 视图。这里我使用 UIActionsheet Picker 视图。当我单击“优先级”时,文本字段选择器将出现,如屏幕截图所示。这也很好用。
我的问题:当我通过滚动而不使用键盘上的完成按钮或下一步按钮直接从主题文本视图单击优先文本字段时。那么下面的问题来了。
或者当我将 UITextview 移动到帮助主题文本字段或源文本字段中的其他任何文本字段时(在此文本字段中使用了 UIActionsheet。)同样的问题出现了。
见截图 3。
这里,keyboard 和 UIActionsheet 都出现了。这里键盘没有隐藏,它仍然出现。它不会出现在这里,当我将这里的 UITextView 移动到下一个文本字段时,即在 UIActionsheet 选择器中,它只显示选择器视图。
我没有得到什么问题。任何人请告诉我解决方案。
这是一些代码,
@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
这是因为时间问题,动作选择器需要一些时间来重新加载键盘上的选择器
顺便说一句,您应该在此处更改代码并在 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];
}
}
在 else 部分你应该像下面这样延迟一段时间后调用 showPickerWithTitle
方法
else{
[self performSelector:@selector(openPickerWithDelay:) withObject:sender afterDelay:0.5]; // Change 0.5 to whatever you want.
}
添加新方法
-(void) openPickerWithDelay:(id) sender {
[ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
}
否则可以延迟
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
});
我认为以下 IBAction
代码:
- (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];
}
}
...以及以下方法:
- (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;
}
...将为您解决问题。
先在这里查看3张截图。
屏幕截图 1:主题是 UITextview 字段。当我点击主题文本字段时,将出现正常键盘。现在这里没有问题了。
屏幕截图 2:优先级为 UITextfield 视图。这里我使用 UIActionsheet Picker 视图。当我单击“优先级”时,文本字段选择器将出现,如屏幕截图所示。这也很好用。
我的问题:当我通过滚动而不使用键盘上的完成按钮或下一步按钮直接从主题文本视图单击优先文本字段时。那么下面的问题来了。
或者当我将 UITextview 移动到帮助主题文本字段或源文本字段中的其他任何文本字段时(在此文本字段中使用了 UIActionsheet。)同样的问题出现了。
见截图 3。
这里,keyboard 和 UIActionsheet 都出现了。这里键盘没有隐藏,它仍然出现。它不会出现在这里,当我将这里的 UITextView 移动到下一个文本字段时,即在 UIActionsheet 选择器中,它只显示选择器视图。
我没有得到什么问题。任何人请告诉我解决方案。
这是一些代码,
@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
这是因为时间问题,动作选择器需要一些时间来重新加载键盘上的选择器
顺便说一句,您应该在此处更改代码并在 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];
}
}
在 else 部分你应该像下面这样延迟一段时间后调用 showPickerWithTitle
方法
else{
[self performSelector:@selector(openPickerWithDelay:) withObject:sender afterDelay:0.5]; // Change 0.5 to whatever you want.
}
添加新方法
-(void) openPickerWithDelay:(id) sender {
[ActionSheetStringPicker showPickerWithTitle:@"Select Priority" rows:_priorityArray initialSelection:0 target:self successAction:@selector(priorityWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
}
否则可以延迟
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
});
我认为以下 IBAction
代码:
- (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];
}
}
...以及以下方法:
- (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;
}
...将为您解决问题。