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(, , 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监听输入特定字符跳转页面选择选项返回的更多相关文章
- 在EditText中限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- EditText 限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- js 监听后退事件及跳转页面
//直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window. ...
- Android EditText截获与监听输入事件
Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...
- EditTextUtil 监听输入字数
package com.toge.ta.utils; import android.text.Editable;import android.text.Selection;import android ...
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...
- 用jquery监听输入数字的变化
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Django进入监听端口就自动打开指定页面,无需导航栏手动添加(Django六)
在我们进入监听端口时画面如下:而因为在urls.py中写过如下语句 我们在监听端口后加上/login就会跳转到login.html页面,如下图 那么如何一打开监听端口就可以走动跳转到login.htm ...
- 小程序webview跳转页面后没有返回按钮完美解决方案
随着小程序越来越火爆,使一个产品如果只有公众号H5页面和APP显得不怎么完美,总感觉不搭上小程序这趟流量车,就会少了点什么,心里别扭地很.在此驱动下,我所在公司也决定赶紧上车. 但是,如果要按照小程序 ...
随机推荐
- VSCode远程连接Docker
一.Docker开启远程访问 [root@local host ~]# vi /lib/systemd/system/docker.service #修改ExecStart这行 ExecStart=/ ...
- IQ基础
I: in-phase 表示同相Q: quadrature 表示正交,与I 相位差90 度. 现在来解释IQ信号的来源: 最早通讯是模拟通讯,假设载波为cos(a),信号为cos(b),那么通过相 ...
- Loadrunner:录制APP脚本
一.问题 使用 Loadrunner 12 自带的代理进行录制APP脚本时,遇到了各种阻碍 二.解决途径 使用 Fiddler 抓包后导出成 saz 格式文件,再导入到 Loadrunner 中,完美 ...
- 王道机试指南题解(C/C++版)
第 2 章 经典入门 一 排序 例 2.1 排序 代码 2.1 冒泡排序(时间复杂度 \(O(n^2)\)) #include <iostream> using std::cin; usi ...
- java——适配器模式、策略模式
适配器模式: https://www.cnblogs.com/honger/p/5970283.html 策略模式: https://www.jianshu.com/p/3bcf55cf83d3
- EntityManager的merge()方法
EntityManager的merge()方法相当于hibernate中session的saveOrUpdate()方法: 用于实体的插入和更新操作:
- MessagePack Java Jackson 在不关闭输入流(input stream)的情况下反序列化多变量
com.fasterxml.jackson.databind.ObjectMapper 在读取输入流变量的时候默认的将会关闭输入流. 如果你不希望关闭输入流,你可以设置 JsonParser.Feat ...
- ACM-ICPC 2018 沈阳赛区网络预赛 I 题 Lattice's basics in digital electronics
原题链接:https://nanti.jisuanke.com/t/31450 附上队友代码:(感谢队友带飞) #include <bits/stdc++.h> using namespa ...
- 2019牛客暑期多校训练营(第一场)H 线性基+计算贡献
题意 给n个整数,求满足子集异或和为0的子集大小之和. 分析 将问题转化为求每个元素的贡献次数之和. 先对n个数求线性基,设线性基大小为r,即插入线性基的数字个数为r,可以分别计算线性基内数的贡献和线 ...
- 记一次protobuf和hbase自带protobuf版本冲突的解决
使用protobuf生产模板代码,使用的版本是: <dependency> <groupId>com.google.protobuf</groupId> <a ...