UIAlertView study
2015-08-14 13:49
344 查看
首先要自定义一个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 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];
}
}
@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
.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 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];
}
}
@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
相关文章推荐
- UITableView的section设置了headerView导致下滑的时候headerView跟着滚动的解决方法
- 在hibernate+mysql:Field 'id' doesn't have a default value
- 《深入理解Android 卷III》第七章 深入理解SystemUI
- 【UISwitch开关控件】
- Codeforces 570 D. Tree Requests
- HDU_1711 Number Sequence(KMP)
- uva 10099 The Tourist Guide
- apue.h头文件(UNIX环境高级编程)
- apue.h头文件(UNIX环境高级编程)
- Codeforces 559B Equivalent Strings 想法题
- BIGINT UNSIGNED value is out of range in..的解决方法
- FIneCMS /dayrui/libraries/Chart/ofc_upload_image.php Arbitrary File Upload Vul
- UVA 题目1521 GCD Guessing Game(数论+贪心)
- poj 2299 Ultra-QuickSort(树状数组)
- xcode工程文件格式(Xcode Project File Format)详解
- php require_once 的使用及与require的区别
- request对象的几个常用方法
- Win7安vc2008编译报LINK : fatal error LNK1000: Internal error during IncrBuildImage
- No enclosing instance of type WallpaperService is available due to some intermediate constructor inv
- 封装同步的UIActionSheet