簡體   English   中英

使UItextField文本不可選擇,同時仍然可以與文本字段進行交互

[英]Make the UItextField text non selectable while still being able to interact with the textfield

需要防止這種類型的UI交互我有一個文本字段,在該文本字段上選擇顯示UIPicker。 選擇器辭職第一響應者后,將從UIPicker選擇的項目顯示在textField中。 但是,如果我長按UITextField,它將為我提供選擇textField文本的選項。

任何變通辦法或解決方案將不勝感激。

 UITextField *CatpickerField = [[UITextField alloc] initWithFrame:self.categoryView.bounds];
CatpickerField.backgroundColor = [UIColor clearColor];
[[CatpickerField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];


CatpickerField .textAlignment = NSTextAlignmentRight;
CatpickerField.text= @"Category";
UIPickerView * pickerCAt = [UIPickerView new];
pickerCAt.backgroundColor = [UIColor whiteColor];
pickerCAt.tag=1001;
pickerCAt.delegate = self;
pickerCAt.dataSource = self;
pickerCAt.showsSelectionIndicator = NO;

UIToolbar *toolBarcat= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar1 setBarStyle:UIBarStyleBlack];
UIBarButtonItem *buttonCancelcat=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain  target:self action:@selector(barButtonCancelAction:)];
UIBarButtonItem *buttonDonecat=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain  target:self action:@selector(barButtonAction:)];
UIBarButtonItem *flexiblecat = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBarcat.items = [NSArray arrayWithObjects:buttonCancelcat,flexiblecat,buttonDonecat,nil];

CatpickerField.inputView = pickerCAt;
CatpickerField.inputAccessoryView = toolBar1;

self.CatPickerviewField = CatpickerField;
[self.categoryView addSubview:self.CatPickerviewField];

1個

將LongPressGestureRecognizer添加到UITextField。

UILongPressGestureRecognizer *lpgr 
   = [[UILongPressGestureRecognizer alloc]
                 initWithTarget:self action:@selector(enableTextField)];
lpgr.delegate = self;
lpgr.delaysTouchesBegan = YES;
[self.textField addGestureRecognizer:lpgr];

並處理:

 -(void)enableTextField
  {
     textField.enabled = YES;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM