iOS UItextView监听输入特定字符跳转页面选择选项返回
Posted 玉思盈蝶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UItextView监听输入特定字符跳转页面选择选项返回相关的知识,希望对你有一定的参考价值。
今天有朋友问我一个需求的实现,于是自己写了一个Demo简单的实现了一下:
需求是:
1>比如: 检测用户输入"A"字符串,跳转页面选择选项,将选择的选项放置textView里,作为当前的输入;
2>不是"A"字符,则正常的textView输入;
3.用户跳转选择了,则将选择的输入到textView,否则,还是输入用户输入的字符即可.
实现代码如下:
// // ViewController.m // Demo // // Created by 思 彭 on 2017/4/30. // Copyright ? 2017年 思 彭. All rights reserved. // #import "ViewController.h" #import "ViewController1.h" @interface ViewController ()<UITextViewDelegate> @property (nonatomic, strong) UITextView *textView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 200)]; self.textView.delegate = self; self.textView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:self.textView]; } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { return YES; } - (BOOL)textViewShouldEndEditing:(UITextView *)textView { return YES; } - (void)textViewDidBeginEditing:(UITextView *)textView { } - (void)textViewDidEndEditing:(UITextView *)textView { } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSLog(@"%@",text); __block BOOL isChangeFlag; if ([text isEqualToString:@"A"]) { // 跳转页面 ViewController1 *vc1 = [ViewController1 new]; vc1.block = ^(NSString *str) { NSLog(@"str = %@",str); NSString *textViewText = textView.text; NSString *s = [textViewText substringWithRange:NSMakeRange(0, textViewText.length)]; if (str.length > 0) { isChangeFlag = YES; textView.text = [NSString stringWithFormat:@"%@%@",s, str]; } else { isChangeFlag = NO; textView.text = [NSString stringWithFormat:@"%@%@",s, text]; } }; [self.navigationController pushViewController: vc1 animated:YES]; return isChangeFlag; } return YES; } - (void)textViewDidChange:(UITextView *)textView { } - (void)textViewDidChangeSelection:(UITextView *)textView { } @end
跳转页面选择:
// // ViewController1.m // Demo // // Created by 思 彭 on 2017/4/30. // Copyright ? 2017年 思 彭. All rights reserved. // #import "ViewController1.h" @interface ViewController1 ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, copy) NSString *selectedStr; @end @implementation ViewController1 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.selectedStr = @""; [self setUI]; } #pragma mark - 设置界面 - (void)setUI { self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.tableFooterView = [[UIView alloc]init]; // 注册cell [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; [self.view addSubview: self.tableView]; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row]; cell.textLabel.font = [UIFont systemFontOfSize:15]; return cell; } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001f; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.0001f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; self.selectedStr = str; if (self.block) { self.block(self.selectedStr); } [self.navigationController popViewControllerAnimated:YES]; } @end
总结:
其实主要的实现就在textView的代理的实现:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
以上是关于iOS UItextView监听输入特定字符跳转页面选择选项返回的主要内容,如果未能解决你的问题,请参考以下文章
Swift 4 或 Swift 5 中的 UITextView 用户输入文本验证:限制 Swift 中的特定字符
无法从 iOS 中的 UITextView 获取已删除的字符