iphone 模仿百度输入框提示

本文详细介绍了如何在iPhone应用中使用UITextField控件,通过模仿百度搜索提示功能,确保用户输入的E-mail地址格式正确。具体实现包括初始化控件、监听输入事件、构建提示列表并动态更新,最后展示符合格式的提示选项供用户选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#import <UIKit/UIKit.h>


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

{

UITextField *mailTextField;

UITableView *myTableView;

NSMutableArray *promptarray;

NSMutableArray *myArr;


}

@property (nonatomic,retain) IBOutlet 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;

    [super viewDidLoad];

}

-(IBAction) textFieldDoneEditing:(id)sender

{

[sender resignFirstResponder];

}


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

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

self.myArr=[[NSMutableArray alloc] init];

[mailTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

}


//结束输入的时候

-(void)textFieldDidEndEditing:(UITextField *)textField

{

myTableView.hidden=YES;

}


//显示提示的选项

- (void) textFieldDidChange:(UITextField *) TextField

{

myTableView.hidden=YES;

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

[self.myArr removeAllObjects];

int i;

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

{

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

[add appendString:[promptarray objectAtIndex:i]];

[self.myArr addObject:add];

}

myTableView.hidden=NO;

[myTableView reloadData];

}

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

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

{

[mailTextField resignFirstResponder];

}


#pragma mark tableView delegate and dataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

{

return 1;

}


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

{

return [self.myArr count];

}


- (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 = [myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: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、付费专栏及课程。

余额充值