iOS UItextView监听输入特定字符跳转页面选择选项返回
生活随笔
收集整理的這篇文章主要介紹了
iOS UItextView监听输入特定字符跳转页面选择选项返回
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今天有朋友問我一個(gè)需求的實(shí)現(xiàn),于是自己寫了一個(gè)Demo簡(jiǎn)單的實(shí)現(xiàn)了一下:
需求是:
1>比如: 檢測(cè)用戶輸入"A"字符串,跳轉(zhuǎn)頁(yè)面選擇選項(xiàng),將選擇的選項(xiàng)放置textView里,作為當(dāng)前的輸入;
2>不是"A"字符,則正常的textView輸入;
3.用戶跳轉(zhuǎn)選擇了,則將選擇的輸入到textView,否則,還是輸入用戶輸入的字符即可.
實(shí)現(xiàn)代碼如下:
// // 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"]) {// 跳轉(zhuǎn)頁(yè)面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跳轉(zhuǎn)頁(yè)面選擇:
// // 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 - 設(shè)置界面- (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];// 注冊(cè)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總結(jié):
其實(shí)主要的實(shí)現(xiàn)就在textView的代理的實(shí)現(xiàn):?
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
?
轉(zhuǎn)載于:https://www.cnblogs.com/pengsi/p/6790423.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的iOS UItextView监听输入特定字符跳转页面选择选项返回的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样快速梦到一个人
- 下一篇: oracle 表空间 用户