关于iOS不同类型键盘的切换

闲言少叙,立马入正题。。。

开发中用pickerView自定义的键盘与系统默认的键盘之间的切换。希望能帮到要用的朋友。。。

#import "ViewController.h"

 

@interface ViewController () <UITextViewDelegate, UITextFieldDelegate, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

 

 

@property (weak, nonatomic) IBOutlet UITextView *textView;

 

 

/** 自定义键盘 */

@property (nonatomic, strong) UIPickerView *pickerView;

 

/** 原因数组 */

@property (nonatomic, strong) NSArray *reasonArray;

 

@property (nonatomic, assign) BOOL isFirst;

 

@end

 

@implementation ViewController

- (IBAction)click:(id)sender {

    

    [self.textField resignFirstResponder];

}

 

- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    self.textView.delegate = self;

    self.textField.delegate = self;

    self.textField.inputView = self.pickerView;

}

 

 

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    [self.textField becomeFirstResponder];

}

 

- (void)viewDidDisappear:(BOOL)animated {

    [super viewDidDisappear:animated];

    [self.textField resignFirstResponder];

}

/*****************************************/

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    // 切换键盘

    [self.textField resignFirstResponder];

    self.textField.enabled = NO;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [textView becomeFirstResponder];

    });

    return YES;

}

- (void)textViewDidBeginEditing:(UITextView *)textView {

    self.textField.enabled = YES;

}

 

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    // 切换键盘

    [self.textView resignFirstResponder];

    self.textView.editable = NO;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [textField becomeFirstResponder];

    });

    

    if (_isFirst) {

        [self pickerView:self.pickerView didSelectRow:0 inComponent:1];

        _isFirst = NO;

    }

    return YES;

}

 

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

    self.textView.editable = YES;

}

// 不允许用户输入

- (BOOL)texttitleField:(UITextField *)texttitleField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    return NO;

}

/*******************切换键盘**********************/

 

#pragma mark - UIPickerViewreasonArraySource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 1;

}

 

// 行数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return self.reasonArray.count;

}

 

#pragma mark - UIPickerVeiwDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    return self.reasonArray[row];

}

 

// 选中某一行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {  

    self.textField.text = self.reasonArray[row];

}

#pragma mark - 懒加载

- (UIPickerView *)pickerView {

    if (!_pickerView) {

        _pickerView = [[UIPickerView alloc] init];

        _pickerView.backgroundColor = [UIColor lightGrayColor];

//        _pickerView.backgroundColor = [UIColor whiteColor];

        _pickerView.delegate = self;

    }

    return _pickerView;

}

 

- (NSArray *)reasonArray {

    if (!_reasonArray) {

        _reasonArray = @[@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311",@"121313311"];

    }

    return _reasonArray;

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。