首先要自定義一個UIAlertView擴展類,如MAlertView: php
.h文件 函數
#import <Foundation/Foundation.h> spa
@interface MAlertView : UIAlertView { .net
UITextField *passwdField; code
NSInteger textFieldCount; orm
} get
- (void)addTextField:(UITextField *)aTextField placeHolder:(NSString *)placeHolder; it
@end io
.m文件 class
#import "MAlertView.h"
#define kMAlertViewTextFieldHeight 30.0
#define kMAlertViewMargin 10.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 = [super initWithTitle: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:[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];
}
}
如何使用此擴展類:
- (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