UIAlertView
来源:互联网 发布:mac os xshell 编辑:程序博客网 时间:2024/05/16 17:28
UIAlertview 触发函数事件
显示alert,将代理设为self
- (IBAction)displayAlertView
{
UIAlertView *view = [[UIAlertView alloc]
initWithTitle:"Alert View Demo"
message:"請選擇"
delegate:self
cancelButtonTitle:"取消"
otherButtonTitles:"儲存",@"載入",nil];
[view show];
[view release];
}
{
UIAlertView *view = [[UIAlertView alloc]
initWithTitle:"Alert View Demo"
message:"請選擇"
delegate:self
cancelButtonTitle:"取消"
otherButtonTitles:"儲存",@"載入",nil];
[view show];
[view release];
}
// 回调函数
-(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex {
{
switch(buttonIndex){
case 0:
NSLog(@"Save…");
break;
case 1:
NSLog(@"Load…");
break;
case 2:
NSLog(@"Cancel…");
break;
}
}
@end
-(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex {
{
switch(buttonIndex){
case 0:
NSLog(@"Save…");
break;
case 1:
NSLog(@"Load…");
break;
case 2:
NSLog(@"Cancel…");
break;
}
}
@end
UIAlertView的自动消失,手动消失和自定制消失
转自:http://blog.csdn.net/likendsl/article/details/7514506
UIAlertView的消失本质其实是触发了一个dismiss事件。
触发这个事件有以下两种方法:
1.按钮点击,UIAlertView上如果有按钮,点击任何按钮都会触发该事件,UIAlertView消失;
2.代码模拟点击
- [AlertObject dismissWithClickedButtonIndex:0 animated:NO]
1。创建一个自动消失的UIAlertView,这种消失方法的本质其实就是:使用定时器+代码模拟点击
- -(void) performDismiss:(NSTimer *)timer
- {
- [Alert dismissWithClickedButtonIndex:0 animated:NO];
- [Alert release];
- }
- -(void)presentAlert
- {
- Alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"出错啦!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
- [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
- [Alert show];
- }
2.手动消失
如果你的UIAlertView上有按钮的话,点击任何一个按钮,都会触发一个dismiss事件,引起UIAlertView的消失。
3.自定制的消失,其实就是在自己认为合适的时候模拟点击。
下面这个例子是在textField的delegate执行即点击returnKey且输入昵称合法的时候,UIAlertView消失。
- - (void) enterNickName:(NSString*)msg
- {
- prompt = [[UIAlertViewalloc] initWithTitle:@"设置昵称"
- message:msg
- delegate:self
- cancelButtonTitle:nil
- otherButtonTitles:nil];
- UITextField *textField = [[UITextFieldalloc] initWithFrame:CGRectMake(27.0,70.0, 230.0, 30.0)];
- [textField setBackgroundColor:[UIColorwhiteColor]];
- [textField setPlaceholder:@"输入昵称"];
- textField.layer.cornerRadius=6.0;
- textField.delegate=self;
- textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
- [prompt addSubview:textField];
- [textField becomeFirstResponder];
- textField.keyboardType=UIKeyboardTypeDefault;
- textField.keyboardAppearance=UIKeyboardAppearanceDefault;
- textField.returnKeyType=UIReturnKeyDone;
- [textField release];
- [promptsetTransform:CGAffineTransformMakeTranslation(0.0, -40.0)]; //可以调整弹出框在屏幕上的位置
- [prompt show];
- }
- #pragma textField delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- NSLog(@"nickname=%@",textField.text);
- if (nil==textField.text ||0==[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length])
- {
- prompt.message=@"\n\n昵称不能为空!";
- return YES;
- }
- if ([[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]<2
- ||[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]>15)
- {
- prompt.message=@"\n\n昵称只支持2-15个字,请重新设置";
- return YES;
- }
- [prompt dismissWithClickedButtonIndex:0animated:NO];//触发dismiss
- [prompt release];
- }
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIalertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- UIAlertView
- 5天来的总结
- 设计模式之抽象状态模式
- 判断字符是否为中文
- Javascript写入txt和读取txt文件
- java 定时器任务
- UIAlertView
- 内核映像的形成 —— KBuild体系(一)
- 求帮助在线等:jquery uploadify onComplete 没响应
- 图的应用详解-数据结构
- AVR单片机(ATMEGA16)外部中断程序
- JAVA 高效开发
- asp.net frameset 框架页面伸缩功能实现
- jBPM4.4开发(六)-----------fork节点
- Tomcat7.0.22在Windows下详细配置过程