当你打开Safari的时候,输入网址,会有许多候选网址,点击后,自动填充到输入框,进入网页。

打开词典查单词的时候,输入前面部分字母,软件会给出符合的候选单词。

这样做的目的,是为了省去用户繁琐的输入,节省时间,提升用户体验。

  先上效果图

今天对基本的UITextField进行改装,让其具备此功能。

新建项目后,在Main.storyboard里,放好UItextField和UIButton。

下一步,使用control+drag将UITextField拖到interface文件里,选择outlet,名为*urlField

同理,UIButton拖进去,选择IBAction,名为goPressed

先引入将要使用的3个委托,两个是UITableView有关,一个用于收回键盘

建立两个Array, pastUrls和autocompleteUrls

建立一个UITableView用于呈现候选数据,下面是interface内代码

#import <UIKit/UIKit.h>

@class WebViewController;

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> 

@property (nonatomic, retain) UITextField *urlField;
@property (nonatomic, retain) NSMutableArray *pastUrls;
@property (nonatomic, retain) NSMutableArray *autocompleteUrls;
@property (nonatomic, retain) UITableView *autocompleteTableView; - (IBAction)goPressed;
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring; @end

pastUrls放入匹配的数据,初始化UITableView,并将其隐藏

- (void)viewDidLoad {
self.pastUrls = [[NSMutableArray alloc] initWithObjects:@"www.google.com", @"www.cnblog.com", nil];
self.autocompleteUrls = [[NSMutableArray alloc] init]; autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView]; [super viewDidLoad];
}
- (IBAction)goPressed:(id)sender {

  // 按下button,收回键盘,隐藏UITableView
[urlField resignFirstResponder];
autocompleteTableView.hidden = YES; // Add the URL to the list of entered URLS as long as it isn't already there
if (![pastUrls containsObject:urlField.text]) {
[pastUrls addObject:urlField.text];
} }
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

  // Put anything that starts with this substring into the autocompleteUrls array
// 过滤,剩下符合输入文字的候选
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
} #pragma mark UITextFieldDelegate methods
//当用户增,删字符的时候,都会调用此方法
//
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO; NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return autocompleteUrls.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
} cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
return cell;
} #pragma mark UITableViewDelegate methods
//将用户选择的候选网址,设置到输入框里,并调用button的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
urlField.text = selectedCell.textLabel.text; [self goPressed]; }

最新文章

  1. Oracle数据加载之外部表的介绍
  2. struts2中各个jar包作用
  3. jquery插件开发继承了jQuery高级编程思路
  4. python学习第二天第一部分
  5. java中vector与hashtable操作详解
  6. NYOJ-79 拦截导弹 AC 分类: NYOJ 2014-01-01 23:25 167人阅读 评论(0) 收藏
  7. [转]Java数组初始化详解
  8. Git 基础再学习之:git checkout -- file
  9. Python新手学习基础之条件语句——elif语句
  10. Flashback Version/Transaction Query
  11. 读书笔记—CLR via C#章节1-2
  12. 《连载 | 物联网框架ServerSuperIO教程》- 15.数据持久化接口的使用。附:3.2发布与版本更新说明。
  13. python文件处理相关函数
  14. ------ Tor(洋葱路由器)匿名网络源码分析——主程序入口点(一)------
  15. python 外键用法 多对多关系 ORM操作 模板相关
  16. 『流畅的Python』第5章笔记_一等函数
  17. css3-文本新增属性
  18. PHP empty、isset、isnull的区别
  19. b2_trsd_EDSD_new
  20. NYOJ-61 传纸条(一)

热门文章

  1. 【Linux】鸟哥的Linux私房菜基础学习篇整理(四)
  2. HDU 5501 背包问题
  3. (转载)HTML标签&lt;br&gt;&lt;br/&gt;的区别在哪里?
  4. 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
  5. 《Euclidea3》-Eta-07
  6. 6种GET和POST请求发送方法
  7. android 客户端 和 新浪微博如何打通的
  8. python字符串连接的三种方法及其效率、适用场景详解
  9. FACTORY设计模式【让吃货也能理解的程序】
  10. [Angular 2] Validation