代码改变世界

警告框扩展应用:给UIAlertView添加UITextField模拟登录框

  张智清  阅读(2815)  评论(0编辑  收藏  举报

首先要自定义一个UIAlertView扩展类,如MAlertView:

复制代码
View Code
// MAlertView.h
//
#import <Foundation/Foundation.h>

@interface MAlertView:UIAlertView {
UITextField *passwdField;
NSInteger textFieldCount;
}

- (void)addTextField:(UITextField *)aTextField placeHolder:(NSString *)placeHolder;

@end

// MAlertView.m
//
#import "MAlertView.h"

#define kMAlertViewTextFieldHeight 30.0;
#define kMAlertViewMargin 10.0

@implementation MAlertView
- (void)initialize {
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,... {
if((self == [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButttonTitles,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:[UIImageView class]]) {

}
else if ([view isKindOfClass:[UILabel class]]) {

rect = view.frame;
maxLabelY = rect.origin.y + rect.size.height;
}
else if ([view isKindOfClass:[UITextField class]]) {

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;
[self addSubview:aTextField];
// [self setNeedsLayout];
}
}

@end
复制代码

如何使用此扩展类:

MAlertView  *alert = [[MAlertView alloc] initWithTitle:@"Title" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];

[alert addTextField:accountField placeHolder:@"Account"];
[alert addTextField:passwdField placeHolder:@"Password"];
[alert show];
[alert release];

效果如图。


进行了简单的封装,只需要用addTextField:placeHolder:方法将textField加进去就好了,其他使用方法和UIAlertView完全一样。
再作些补充说明:因为UIAlertView只有在点击了按钮才能进行交互,所有的text值都是在点击之后获取对应的textField的值。
有些朋友说空值的情况,这个可以在点击之后判断如果是空值就再次弹出alertView就可以了。

以上是通过自定义UIAlertView扩展类来实现UIAlertView模拟登录框的。现在最新的iOS5.0的UIAlertView类控件本身已经可以通过设置属性alertViewStyle来实现了。这一属性可选类型如下:

typedef enum {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
}UIAlertViewStyle



本文整理转载自:http://www.cocoachina.com/bbs/read.php?tid=86733

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示