UIAlertView中加入UITextField及Username/Password彈跳視窗 分类: iOS UIAlertView 代码实现

转自:http://blog.csdn.net/me10net/article/details/7937560

UIAlertView中加入UITextField  

这个简单的教程将介绍怎样在UIAlertView中加入UITextField。它只包括几行代码。你将学到CGAffineTransform和UITextField编程。

Screen shot 2009-11-09 at 8.12.11 AM

 

我们开始吧…

1. 建立一个新的View Based项目。
你可以随意为其取名,我为其命名为TextFieldInAlert。

2. 代码
在viewcontroller.m或TextFieldInAlert.m(如果你命名为TextFieldInAlert的话),然后找到-(void)viewDidLoad 方法。取消其注释加入代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)viewDidLoad {

    [super viewDidLoad];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Enter Name Here” 
        message:@”this  gets covered!” delegate:self 
        cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];

    UITextField *myTextField = [[UITextField alloc] 
        initWithFrame:CGRectMake(12, 45, 260, 25)];

    [myTextField setBackgroundColor:[UIColor whiteColor]];

    [alert addSubview:myTextField];

    [alert show];

    [alert release];
    [myTextField release];

}

 

我们做的是调用UIAlertView然后添加一个UITextField。 你可能注意到了UIAlertView的message部分,我们为其赋值为“this gets covered!”,如果我们不为其加上这个语句,那么alert的按钮会更靠上而弄乱UITextField。你可以试一下拿掉这一行看看会发生什么。 现在UITextField 出现在UIAlertView中间了。试一下在UITextField输入点什么。噢,为什么键盘盖住了UIAlertView?有一个简单的修正方法。 只需加入两行代码就可修正这个问题。加入代码:

1
2
3
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);

    [alert setTransform:myTransform];

 

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Enter Name Here” 
        message:@”this  gets covered!” delegate:self 
        cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];

    UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];

    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);

    [alert setTransform:myTransform];

    [myTextField setBackgroundColor:[UIColor whiteColor]];

    [alert addSubview:myTextField];

    [alert show];

    [alert release];

    [myTextField release];

 

现在你按下“Build and Run”,你会注意到UITextField将在显示略高一点的位置,当你在UITextField输入时,键盘不会再盖住。这就是CGAffineTransform的作用。

Username/Password彈跳視窗(UIAlertView)

  1. 首先,你要初始化一個UIAlertView物件。然後你必須將message的參數值設為@”\n\n\n”,這是用來多留幾行空白以便擺放輸入框欄位。
  2. 建立兩個UITextField物件,將他們加至UIAlertView裡成為subview。別忘了設定輸入框的背景色(setBackgroundColor)、預設文字(setPlaceholder)、安全表示(setSecureTextEntry,輸入文字時會變成星號,用於密碼欄)。
  3. 最後,在呈現UIAlertView時,在垂直位移加上一點Shift值並且有需要的話將之設定為FirstResponder。

Source Code:

01. UITextField *textField;
02. UITextField *textField2;
03.    
04. UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Username and password"
05. message:@"\n\n\n" // IMPORTANT
06.  delegate:nil
07.          cancelButtonTitle:@"Cancel"
08.          otherButtonTitles:@"Enter", nil];
09.    
10. textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
11. [textField setBackgroundColor:[UIColor whiteColor]];
12. [textField setPlaceholder:@"username"];
13. [prompt addSubview:textField];
14.    
15. textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
16. [textField2 setBackgroundColor:[UIColor whiteColor]];
17. [textField2 setPlaceholder:@"password"];
18. [textField2 setSecureTextEntry:YES];
19. [prompt addSubview:textField2];
20.    
21. // set place
22. [prompt setTransform:CGAffineTransformMakeTranslation(0.0, 110.0)];
23. [prompt show];
24. [prompt release];
25.    
26. // set cursor and show keyboard
27. [textField becomeFirstResponder];

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值