今天有朋友问我一个需求的实现,于是自己写了一个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. Android异常与性能优化相关面试问题-冷启动优化面试问题详解

    什么是冷启动: 冷启动的定义:冷启动就是在启动应用前,系统中没有该应用的任何进程信息.实际也就是要执行Application.onCreate()方法的那次启动. 冷启动 / 热启动的区别:热启动:用 ...

  2. Navicat for Mysql查询结果导出无表名

    在查询窗口用select语句按条件查出所需结果,然后用“导出向导”把查询结果导成sql文件,但是导出来的sql语句没有表名了. 导成的sql文件大致是这样的, INSERT INTO `` (`id` ...

  3. C++中虚函数继承类的内存占用大小计算

    计算一个类对象的大小时的规律: 1.空类.单一继承的空类.多重继承的空类所占空间大小为:1(字节,下同): 2.一个类中,虚函数本身.成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空 ...

  4. 记一次Python导包经历

    最近由于需要写一个脚本调用另一个文件里面的一个方法,试了很久都导包失败,特此记录一下 问题背景 1)脚本文件为send_reward.py,要调用public_model_func.py里面的一个类方 ...

  5. (转载) Consul 使用手册(感觉比较全了)

    使用consul 介绍 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul的客户端可用提供一个服务,比如 api 或者 ...

  6. 【leetcode】1276. Number of Burgers with No Waste of Ingredients

    题目如下: Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as ...

  7. BZOJ 3060: [Poi2012]Tour de Byteotia 并查集

    前 $k$ 个节点形成的结构必定是森林,而 $[k+1,r]$ 之间肯定是都连上,而剩下的一个在 $[1,k],$一个在 $[k+1,r]$ 的节点就能连多少连多少即可. Code: #include ...

  8. Struts增删改查

    1.导入相关的pom依赖(struts.自定义标签库的依赖) <dependency> <groupId>jstl</groupId> <artifactId ...

  9. poj 2976 Dropping tests (最大化平均值:二分查找)

    #include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...

  10. fhq Treap(无旋Treap)

    先吹一波fhq dalao,竟然和我一个姓,我真是给他丢脸. 昨天treap就搞了一下午,感觉自己弱爆了.然后今天上午又看了一个上午的无旋treap再次懵逼,我太弱了,orzorz. 所以写个博客防止 ...