UIActionSheet
#import "FirstViewController.h"
@interface FirstViewController ()<UIActionSheetDelegate,UIAlertViewDelegate>
@property (retain, nonatomic) IBOutlet UILabel *aLabel;
@property (retain, nonatomic) IBOutlet UITextField *textField;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)upButton:(UIButton *)sender {
//UIActionSheet
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消按钮" destructiveButtonTitle:@"确认按钮" otherButtonTitles:nil, nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
// 更改标题
sheet.title = @"请点击您的选择";
// 添加按钮
//添加的按钮位置从第三个算起
[sheet addButtonWithTitle:@"添加的按钮"];
//红色按钮位置
sheet.destructiveButtonIndex = 0;
[sheet showInView:self.view];
[sheet release];
//UIAlertView
//一般来说,根据苹果官方制定的《iOS 用户界面指南》,在拥有两个按钮的对话框中,您应当将取消按钮放在左边
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否已满十八岁?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
//UIAlertViewStyleDefault 默认风格,无输入框
//UIAlertViewStyleSecureTextInput 带一个密码输入框
//UIAlertViewStylePlainTextInput 带一个文本输入框
//UIAlertViewStyleLoginAndPasswordInput 带一个文本输入框,一个密码输入框
[alert show];
[alert release];
}
第一种 ActionSheet单独使用
//第一种 ActionSheet单独使用
//接收ActionSheet点击事件
//该方式由UIActionSheetDelegate协议定义,点击ActionSheet的按钮后自动执行
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
self.aLabel.text = self.textField.text;
break;
case 1:
break;
case 2:
NSLog(@"让你点你还真点");
break;
default:
break;
}
}
第二种,单独使用UIAlertView
//接收UIAlertView点击事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
break;
case 1:
self.aLabel.text = self.textField.text;
break;
default:
break;
}
}
第三种混合使用
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSString *string=[NSString stringWithFormat:@"你选择了 %@",[actionSheet buttonTitleAtIndex:buttonIndex]];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"输入账户和密码" message:string delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
//UIAlertViewStyleDefault 默认风格,无输入框
//UIAlertViewStyleSecureTextInput 带一个密码输入框
//UIAlertViewStylePlainTextInput 带一个文本输入框
//UIAlertViewStyleLoginAndPasswordInput 带一个文本输入框,一个密码输入框
switch (buttonIndex) {
case 0:
[alert show];
break;
case 1:
break;
default:
break;
}
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//该方法由UIAlertViewDelegate协议定义,在点击AlertView按钮时自动执行,所以如果这里再用alertView来弹出提示,就会死循环,不停的弹AlertView
// NSString * string=[NSString stringWithFormat:@"你点击了 %@",[alertView buttonTitleAtIndex:buttonIndex]];
NSString * string=[NSString stringWithFormat:@"你点击了 %@",[alertView buttonTitleAtIndex:buttonIndex]];
switch (buttonIndex) {
case 0:
break;
case 1:
self.aLabel.text = self.textField.text;
break;
default:
break;
}
// UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
// [alert show];
NSLog(@"%@",string);
// NSLog(@"输入 %@",[[alertView textFieldAtIndex:0] text]);
//获取第一个文本框输入的文本,如果没有文件框,会异常,索引从0开始
}
<span style="background-color: rgb(255, 0, 0);"><span style="color:#99ff99;">最终效果:</span></span>