闲言少叙,立马入正题。。。
开发中用pickerView自定义的键盘与系统默认的键盘之间的切换。希望能帮到要用的朋友。。。
1 #import "ViewController.h" 2 3 @interface ViewController () <UITextViewDelegate, UITextFieldDelegate, UIPickerViewDelegate> 4 5 @property (weak, nonatomic) IBOutlet UITextField *textField; 6 @property (weak, nonatomic) IBOutlet UITextView *textView; 7 8 /** 自定义键盘 */ 9 @property (nonatomic, strong) UIPickerView *pickerView; 10 11 /** 原因数组 */ 12 @property (nonatomic, strong) NSArray *reasonArray; 13 14 /** 如果是第一次则直接保存值,否则不保存 */ 15 @property (nonatomic, assign) BOOL isFirst; 16 17 @end 18 19 @implementation ViewController 20 21 - (IBAction)click:(id)sender { 25 [self.textField resignFirstResponder]; 27 } 28 29 - (void)viewDidLoad { 30 31 [super viewDidLoad]; 33 self.textView.delegate = self; 35 self.textField.delegate = self; 37 self.textField.inputView = self.pickerView; 39 } 40 41 - (void)viewDidAppear:(BOOL)animated { 43 [super viewDidAppear:animated]; 45 [self.textField becomeFirstResponder]; 47 } 48 49 - (void)viewDidDisappear:(BOOL)animated { 51 [super viewDidDisappear:animated]; 53 [self.textField resignFirstResponder]; 55 } 56 57 /*****************************************/ 58 #pragma mark - UITextViewDelegate 59 60 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 64 [self.textField resignFirstResponder]; 66 self.textField.enabled = NO; 68 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 70 [textView becomeFirstResponder]; 72 }); 74 return YES; 76 } 77 78 - (void)textViewDidBeginEditing:(UITextView *)textView { 80 self.textField.enabled = YES; 82 } 83 84 #pragma mark - UITextFieldDelegate 85 86 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 88 // 切换键盘 90 [self.textView resignFirstResponder]; 92 self.textView.editable = NO; 94 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 96 [textField becomeFirstResponder]; 98 }); 101 if (_isFirst) { 103 [self pickerView:self.pickerView didSelectRow:0 inComponent:1]; 105 _isFirst = NO; 107 } 109 return YES; 111 } 112 113 - (void)textFieldDidBeginEditing:(UITextField *)textField { 115 self.textView.editable = YES; 117 } 118 119 // 不允许用户输入 121 - (BOOL)texttitleField:(UITextField *)texttitleField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 123 return NO; 125 } 126 127 /*******************切换键盘**********************/ 128 #pragma mark - UIPickerViewreasonArraySource 130 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 132 return 1; 134 } 135 136 // 行数 137 138 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 140 return self.reasonArray.count; 142 } 143 144 #pragma mark - UIPickerVeiwDelegate 145 146 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 148 return self.reasonArray[row]; 150 } 151 152 // 选中某一行 153 154 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 156 self.textField.text = self.reasonArray[row]; 158 } 159 160 #pragma mark - 懒加载 161 162 - (UIPickerView *)pickerView { 164 if (!_pickerView) { 166 _pickerView = [[UIPickerView alloc] init]; 168 _pickerView.backgroundColor = [UIColor lightGrayColor]; 170 // _pickerView.backgroundColor = [UIColor whiteColor]; 172 _pickerView.delegate = self; 174 } 176 return _pickerView; 178 } 179 180 - (NSArray *)reasonArray { 182 if (!_reasonArray) { 184 _reasonArray = @[@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311"]; 186 } 188 return _reasonArray; 190 }