iphone 的UITextField 模仿百度搜索提示,达到输入正确E-mail地址

本文介绍了一个简单的 iOS 应用场景,通过 UITextField 实现类似百度搜索提示的功能来辅助用户输入正确的电子邮件地址。当用户在文本框中输入时,会自动出现一些常用的邮箱后缀供用户选择。

iphone 的UITextField 模仿百度搜索提示,达到输入正确E-mail地址,详细实现如下:

#import <UIKit/UIKit.h>


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

{

UITextField *mailTextField;

UITableView *myTableView;

NSMutableArray *promptarray;

 

NSMutableArray *myArr;


}

@property (nonatomic,retainIBOutlet UITextField *mailTextField;

@property (nonatomic,retain) IBOutlet UITableView *myTableView;

@property (nonatomic,retain) NSMutableArray *myArr;

-(IBAction) textFieldDoneEditing:(id)sender;

@end


#import "DemoMailViewController.h"


@implementation DemoMailViewController

@synthesize mailTextField,myTableView;

@synthesize myArr;





// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad 

{

myTableView.hidden=YES;

 

    [superviewDidLoad];

 

}

-(IBAction) textFieldDoneEditing:(id)sender

{

[sender resignFirstResponder];

}


//文本框开始输入的时候

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

 

self.myArr=[[NSMutableArrayalloc] init];

 

 

[mailTextFieldaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];

 

}


//结束输入的时候

-(void)textFieldDidEndEditing:(UITextField *)textField

{

myTableView.hidden=YES;

}


//显示提示的选项

- (void) textFieldDidChange:(UITextField *) TextField

{

myTableView.hidden=YES;

 

promptarray=[[NSArrayalloc]initWithObjects:@"@qq.com",@"@163.com",@"@gmail.com",@"@sina.com",@"@foxmail.com",@"@sohu.com",@"@163.com",@"@tom.com",@"@yeah.com",@"@yahoo.com.cn",nil];

 

[self.myArrremoveAllObjects];

 

int i;

for(i=0;i<[promptarray count];i++)

{

NSMutableString *add=[[NSMutableStringalloc] initWithString:TextField.text];

 

[add appendString:[promptarrayobjectAtIndex:i]];

 

[self.myArr addObject:add];

}

 

 

myTableView.hidden=NO;

[myTableView reloadData];

 

}

////随便点击外面刷新试图

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

 

[mailTextFieldresignFirstResponder];

}


#pragma mark tableView delegate and dataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

{

return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{

 

return [self.myArrcount];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


  

return 48;

 

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{  

NSUInteger row=[indexPath row];

static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [myTableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];

    }

if (myArr&&[myArr count]) 

{

cell.textLabel.text =[myArr objectAtIndex:row];; 

}

 

return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 

    mailTextField.text = [myArr objectAtIndex:indexPath.row];


myTableView.hidden=YES;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值