今天有朋友问我一个需求的实现,于是自己写了一个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(, , self.view.frame.size.width - , )];
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(, textViewText.length)];
if (str.length > ) {
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(, , 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 ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ;
} - (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:];
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监听输入特定字符跳转页面选择选项返回的更多相关文章

  1. 在EditText中限制输入,自定义样式,监听输入的字符,自动换行

    自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...

  2. EditText 限制输入,自定义样式,监听输入的字符,自动换行

    自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...

  3. js 监听后退事件及跳转页面

    //直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window. ...

  4. Android EditText截获与监听输入事件

      Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...

  5. EditTextUtil 监听输入字数

    package com.toge.ta.utils; import android.text.Editable;import android.text.Selection;import android ...

  6. 正则表达式——WPF输入控件TextBox 限定输入特定字符

    概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...

  7. 用jquery监听输入数字的变化

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. Django进入监听端口就自动打开指定页面,无需导航栏手动添加(Django六)

    在我们进入监听端口时画面如下:而因为在urls.py中写过如下语句 我们在监听端口后加上/login就会跳转到login.html页面,如下图 那么如何一打开监听端口就可以走动跳转到login.htm ...

  9. 小程序webview跳转页面后没有返回按钮完美解决方案

    随着小程序越来越火爆,使一个产品如果只有公众号H5页面和APP显得不怎么完美,总感觉不搭上小程序这趟流量车,就会少了点什么,心里别扭地很.在此驱动下,我所在公司也决定赶紧上车. 但是,如果要按照小程序 ...

随机推荐

  1. hashmap C++实现分析及std::unordered_map拓展

    今天想到哈希函数,好像解决冲突的只了解了一种链地址法而且也很模糊,就查了些资料复习一下 1.哈希Hash 就是把任意长度的输入,通过哈希算法,变换成固定长度的输出(通常是整型),该输出就是哈希值. 这 ...

  2. 第六章 组件 61 动画-小球动画flag标识符的作用分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. AspectJ注解支持

    <aop:aspectj-autoproxy/> 配置aspectj启动 AspectJAutoProxyBeanDefintionParser implements BeanDefini ...

  4. 数据管理必看!Kendo UI for jQuery过滤器状态保持

    Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...

  5. 源码安装 qemu-2.0.0 及其依赖 glib-2.12.12

    源码安装qemu-2.0.0 下载源代码并解压 http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 .tar.gz 编译及安装: cd q ...

  6. 常用NoSql数据库比较

    1. CouchDB 所用语言: Erlang 特点:DB一致性,易于使用 使用许可: Apache 协议: HTTP/REST 双向数据复制, 持续进行或临时处理, 处理时带冲突检查, 因此,采用的 ...

  7. SpringBoot对接收及返回Instant类型的处理(转)

    一:处理post请求json中的Instant类型1.springboot中日期格式化配置: spring: jackson: date-format: yyyy-MM-dd HH:mm:ss tim ...

  8. 【Winform-自定义控件】 DataGridView多维表头

    [datagridview与treeview绑定] treeview            代码: DataTable dtable = new DataTable("Rock") ...

  9. springboot中spring.profiles.include的妙用

    我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改 ...

  10. 第一次 CSP-S 的游记

    菜得很啊菜得很! --PinkRabbit 第一次 CSP-S ,真的是 第一次. 作为一名初三学生,虽然是 第一次 参加 和NOIP没有任何关系 的 CSP-S ,总是要有点目标呀. 第一试 因为是 ...