QQ:188816190,欢迎交流。
创建:
UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:@"提示!"message:@"用户名或密码错误!" delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定1",@"确定2",nil];
举例:
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"DefaultAlert View" message:@"Defalut"delegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",nil];
标准的双按钮,cancel那个buttonIndex 为0, ok button 的buttonIndex为1
UIAlertView*alertView =[[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“Thiron”, nil];
和程序里的顺序一样,cancel ok thiron 的buttonIndex 分别为0 1 2
UIAlertView*alertView =[[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“Thiron”, nil];
同理,cancel ok thironFourthButton的buttonIndex 分别为0 1 2 3
[alertView show];
从UIAlertView的定义可以看出UIAlertView也是UIView的子类,并且定义在IOS2.0以上的版本。
下面对其定义进行说明,然后举一个例子说明常用用法:
- (id)initWithTitle:(NSString*)title
message:(NSString *)message
delegate:(id )delegate
cancelButtonTitle:(NSString*)cancelButtonTitle
otherButtonTitles:(NSString*)otherButtonTitles, ... ,nil;
初始化一个UIAlertView,并且指定标题(title)、显示的文字(message)、委托对象(需要实现UIAlertViewDelegate协议。一般为self,所以这个ViewController对象需要实现这个协议)、取消按钮的文字(cancelButtonTitle)、其它按钮的显示文字(多个的话使用逗号分隔开,以nil结尾)
@property(nonatomic,assign) iddelegate; // 委托对象
@property(nonatomic,copy) NSString *title; //标题文字
@property(nonatomic,copy) NSString *message; // 显示的消息文本文字
- (NSInteger)adonWithTitle:(NSString*)title;
添加一个Button到AlertView并且指定按钮显示的文字,并且返回它的索引(从0开始,cancelButton的索引是0)
- (NSString*)buttonTitleAtIndex:(NSInteger)buttonIndex;
返回指定索引值的按钮的显示文本
@property(nonatomic,readonly) NSIntegernumberOfButtons;
返回NSAlertView中的所有的按钮的数量
@property(nonatomic) NSInteger cancelButtonIndex;
@property(nonatomic,readonly) NSIntegerfirstOtherButtonIndex;
@property(nonatomic,readonly,getter=isVisible) BOOLvisible;
- (void)show;
显示AlertView
-(void)dismissWithClickeonIndex:(NSInteger)buttonIndexanimated:(BOOL)animated;
隐藏按下指定索引值的按钮之后,隐藏AlertView,并制定是否启动动画效果
@property(nonatomic,assign) UIAlertViewStylealertViewStyle NS_AVAILABLE_IOS(5_0);
- (UITextField*)textFieldAtIndex:(NSInteger)textFieldIndexNS_AVAILABLE_IOS(5_0);
返回指定索引值的TextField ,这个API仅存在于IOS5.0以上
------------------------------------------------------------------------------------------------------------------------------------------------
下面说一下UIAlertViewDelegate协议,这个协议实现了NSObject非正式协议,没有必须实现的方法,所有的方法都是可选的
@protocol UIAlertViewDelegate
@optional
- (void)alertView:(UIAlertView *)alertViewclickeonAtIndex:(NSInteger)buttonIndex;
当一个指定索引的按钮被点击的时候,回调此方法,buttonIndex是按钮的索引值,从0开始
// Called when we cancel a view (eg. the user clicksthe Home button). This is not called when the user clicks thecancel button.
// If not defined in the delegate, we simulate aclick in the cancel button
- (void)alertViewCancel:(UIAlertView*)alertView;
当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法
- (void)willPresentAlertView:(UIAlertView*)alertView;
开始显示View的动画之前进行回调
- (void)didPresentAlertView:(UIAlertView*)alertView;
显示动画完成之后进行回调
- (void)alertView:(UIAlertView *)alertViewwillDismissWithButtonIndex:(NSInteger)buttonIndex;
将要开始View隐藏动画的时候进行回调
- (void)alertView:(UIAlertView *)alertViewdidDismissWithButtonIndex:(NSInteger)buttonIndex;
当View的隐藏动画结束的时候进行回调
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView;
编辑任何默认的字段添加的风格之后调用
例子,这个例子在点击按钮的时候,修改UILabel的文字,并且弹出对话框
1
2
3
4
5
6
7
8
9
10
|
#import
@interface
XinYeViewController : UIViewController
@property
(weak,
nonatomic
)
IBOutlet
UIButton *myButton;
@property
(weak,
nonatomic
)
IBOutlet
UITextField *myTextField;
@property
(weak,
nonatomic
)
IBOutlet
UILabel *myLabel;
-(
IBAction
)clickMyButton:(
id
)sender;
@end
|
#import "XinYeViewController.h"
@interface
XinYeViewController ()
@end
// 点击按钮的次数
static
int
CLICK_TIMES =1;
@implementation
XinYeViewController
@synthesize
myButton;
@synthesize
myLabel;
@synthesize
myTextField;
-(
void
)viewDidLoad
{
[
super
viewDidLoad];
[myTextFieldbecomeFirstResponder];
//一旦进入见面就显示软键盘(myTextField获得焦点)
}
-(
void
)didReceiveMemoryWarning
{
[
super
didReceiveMemoryWarning];
}
-(
IBAction
)clickMyButton:(
id
)sender{
CLICK_TIMES+= 1;
if
(CLICK_TIMES% 2 == 0){
[myLabelsetText:@
"修改后的文字,modify byxinye"
];
}
else
{
[myLabelsetText:@
"不要再点了,你烦不烦啊!!!"
];
}
[[[UIAlertViewalloc] initWithTitle:@
"这里是标题"
message:@
"这里显示的是Message信息"
delegate:
self
cancelButtonTitle:@
"Cancel按钮"
otherButtonTitles:@
"OK"
,@
"Hello"
,@
"World"
,
nil
]show];
}
// 在这里处理UIAlertView中的按钮被单击的事件
-(
void
)alertView:(UIAlertView *)alertViewclickeonAtIndex:(
NSInteger
)buttonIndex
{
NSLog
(@
"buttonIndexis : %i"
,buttonIndex);
switch
(buttonIndex) {
case
0:{
}
break
;
case
1:{
}
break
;
case
2:{
}
break
;
case
3:{
}
break
;
default
:
break
;
}
}
// 当点击软键盘的Enter键的时候进行回调
-(
BOOL
)textFieldShouldReturn:(UITextField*)textField
{
[myTextFieldresignFirstResponder];
// 让myTextField失去焦点
return
YES
;
}
@end
|
运行效果如下: