给UIAlertView添加UITextField模拟登录框
来源:互联网 发布:ubuntu 16.04 fcitx 编辑:程序博客网 时间:2024/05/23 11:59
首先要自定义一个UIAlertView扩展类,如MAlertView:
.h文件
#import<Foundation/Foundation.h>
@interface MAlertView :UIAlertView {
UITextField *passwdField;
NSInteger textFieldCount;
}
- (void)addTextField:(UITextField *)aTextField placeHolder:(NSString *)placeHolder;
@end
.m文件
#import"MAlertView.h"
#define kMAlertViewTextFieldHeight30.0
#define kMAlertViewMargin10.0
@implementation MAlertView
- (void)initialize{
}
//2 buttons supported at most
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...{
if ((self = [superinitWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil])) {
}
return self;
}
- (void)layoutSubviews{
CGRect rect = self.bounds;
rect.size.height += textFieldCount*(kMAlertViewTextFieldHeight +kMAlertViewMargin);
self.bounds = rect;
float maxLabelY = 0.f;
int textFieldIndex = 0;
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UIImageViewclass]]) {
}
else if ([view isKindOfClass:[UILabelclass]]) {
rect = view.frame;
maxLabelY = rect.origin.y + rect.size.height;
}
else if ([view isKindOfClass:[UITextFieldclass]]) {
rect = view.frame;
rect.size.width =self.bounds.size.width - 2*kMAlertViewMargin;
rect.size.height = kMAlertViewTextFieldHeight;
rect.origin.x =kMAlertViewMargin;
rect.origin.y = maxLabelY +kMAlertViewMargin*(textFieldIndex+1) + kMAlertViewTextFieldHeight*textFieldIndex;
view.frame = rect;
textFieldIndex++;
}
else { //UIThreePartButton
rect = view.frame;
rect.origin.y =self.bounds.size.height - 65.0;
view.frame = rect;
}
}
}
- (void)addTextField:(UITextField *)aTextField placeHolder:(NSString *)placeHolder{
if (aTextField != nil) {
textFieldCount++;
aTextField.frame =CGRectZero;
aTextField.borderStyle =UITextBorderStyleRoundedRect;
aTextField.placeholder = placeHolder;
[selfaddSubview:aTextField];
// [self setNeedsLayout];
}
}
@end
如何使用此扩展类:
- (void)viewDidLoad
{
[superviewDidLoad];
MAlertView *alert = [[MAlertViewalloc]initWithTitle:@"Title"message:nildelegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",nil];
UITextField* accountField=[[UITextFieldalloc]init];
UITextField* passwdField=[[UITextFieldalloc]init];
[alert addTextField:accountField placeHolder:@"Account"];
[alert addTextField:passwdField placeHolder:@"Password"];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex//按键响应函数
{
NSString* msg = [[NSStringalloc]initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
效果如图。
进行了简单的封装,只需要用addTextField:placeHolder:方法将textField加进去就好了,其他使用方法和UIAlertView完全一样。
再作些补充说明:因为UIAlertView只有在点击了按钮才能进行交互,所有的text值都是在点击之后获取对应的textField的值。
有些朋友说空值的情况,这个可以在点击之后判断如果是空值就再次弹出alertView就可以了
参考:http://www.cocoachina.com/bbs/read.php?tid=86733
下载源代码:http://download.csdn.net/detail/x1135768777/4214283
- 给UIAlertView添加UITextField模拟登录框
- 警告框扩展应用:给UIAlertView添加UITextField模拟登录框
- 往UIAlertView中添加UITextField模拟登录框
- UIAlertView上添加输入框UITextField
- UIAlertView视图中添加UITextField
- UIAlertView视图中添加UITextField
- 给 UITextField 添加左侧指示图片(类似微信登录框)
- 给 UITextField 添加左侧指示图片(类似微信登录框)
- 给UITableViewCell添加UITextField
- 给UIAlertView UIActionSheet 添加block支持
- 给UIAlertView UIActionSheet 添加block支持
- UIAlertView中加入UITextField
- UIAlertView中加入UITextField
- UIAlertView add UITextField
- 给UITextField添加左侧指示图片
- 模拟 UIAlertView
- 给UIAlertView添加手势使点击区域外弹框消失
- day 019 UITextField UIAlertView UIActionSheet
- 第二章 C++笔记
- linux命令----set
- Myeclipse8.6集成svn插件
- 腾讯面试试题,算法案例
- Android中实现滑动翻页—使用ViewFlipper
- 给UIAlertView添加UITextField模拟登录框
- BitSpread
- java程序的换行符/n怎么写?
- 数组名的sizeof 和 指针名的sizeof
- 如何学习c语言
- 菜鸟学堂 -【Oracle 分析函数over(),row_number()】
- 全角转半角
- asp.net filestream 如何读取pdf文件
- ADO.NET 第一个数据库连接