今天有朋友问我一个需求的实现,于是自己写了一个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. 移动端的文本框获取焦点时导致fixed或absolute定位的按钮被手机键盘顶上去的问题

    var win_h = $(window).height();//关键代码 window.addEventListener('resize', function () { if($(window).h ...

  2. Asp.Net MVC4 使用Unity 实现依赖注入

    项目创建参考 上一篇   <<Asp.Net  MVC5  使用Unity 实现依赖注入>>, 不同的是这里是 Unity.MVC4 装好后会出现 然后示例说在这里写对应关系 ...

  3. “美登杯”上海市高校大学生程序设计邀请赛 **D. 小花梨的取石子游戏**

    "美登杯"上海市高校大学生程序设计邀请赛 (华东理工大学) D. 小花梨的取石子游戏 Description 小花梨有

  4. BPR贝叶斯个性化排序算法

    全序关系:集合中的任两个元素之间都可以比较的关系.

  5. python 示例代码2

    示例2:变量赋值,打印拼接(var.py) 变量定义的规则: 变量名只能是字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名 ['and', 'as', 'asse ...

  6. Vue错误集

    1.Component template should contain exactly one root element. If you are using v-if on multiple elem ...

  7. 2019CCPC秦皇岛赛区(重现赛)- J

    链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=872 题意: 鉴纯夏是一名成绩不太好的高中生. ...

  8. SQL Prompt 5 功能按键说明

    1. Refresh suggestions                 未知,按了没反映 2.Format sql                               标准化SQL代码书 ...

  9. MessagePack Java 0.6.X 可选字段

    你可添加一个新的字段来保持可用性.在新字段中使用 @Optional 注解. @Message public static class MyMessage {     public String na ...

  10. mac使用sublime text3打开当前文件夹的终端

    打开sublime text3,同时按住shift+command+p打开扩展列表, 选择Package Control: Install Pageage,回车. 在输入框输入: terminal,回 ...