说明:蓝色字体的字是五个
butoon
“展示时间”是一个lable
#import "ViewController.h"
@interface ViewController ()<</span>UIAlertViewDelegate,UIActionSheetDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {
UIAlertView *alertView;
NSArray *picker_array;
}
- (IBAction)showAlertView:(id)sender;
- (IBAction)showActionView:(id)sender;
- (IBAction)showPikerView:(id)sender;
- (IBAction)showDateView:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *showL;
- (IBAction)doShowTime:(id)sender;
@end
@implementation ViewController
static int indexN=1;
- (void)viewDidLoad {
[super viewDidLoad];
picker_array = [NSArray arrayWithObjects:@"春",@"夏",@"秋",@"冬", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - 1.UIAlertView警告框
- (IBAction)showAlertView:(id)sender {
alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"这是一个警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"1",@"2",@"3", nil];
alertView.tag = 100;
[alertView adonWithTitle:@"不确定"];//动态添加子按钮
NSLog(@"%d",alertView.visible);//是否可见
NSLog(@"%d",alertView.numberOfButtons);//警告框的子按钮个数
NSLog(@"%@",[alertView buttonTitleAtIndex:0]);//指定索引的按钮的标题
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *loginTF = [alertView textFieldAtIndex:0];
UITextField *pwTF = [alertView textFieldAtIndex:1];
loginTF.placeholder = @"请输入账号";
pwTF.placeholder = @"请输入密码";
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView1 clickeonAtIndex:(NSInteger)buttonIndex{
NSLog(@"OK:tag=%d",(int)alertView1.tag);
switch (buttonIndex) {
case 0:
{
NSLog(@"取消");break;
}
case 1:{
NSLog(@"这是1");break;
}
case 2:{
NSLog(@"这是2");break;
}
case 3:{
NSLog(@"这是3");break;
}
default: break;
}
}
#pragma mark - 2.UIActionSheet提示框
- (IBAction)showActionView:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil];
[actionSheet showInView:self.view];//展示在哪一个视图中
}
//判断点击了哪一个
-(void)actionSheet:(UIActionSheet *)actionSheet clickeonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%d",buttonIndex);
}
//将要消失
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
}
//已经消失
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
}
#pragma mark - 3.UIPickerView选择器
- (IBAction)showPikerView:(id)sender {
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(30, 100, 260, 200)];
picker.delegate = self;//代理
picker.dataSource = self;//数据源的协议(代理)
[self.view addSubview:picker];
}
#pragma mark (3.1) UIPickerViewDataSource中必须实现的方法
//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
//返回行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return 4;
}else
return 3;
}
#pragma mark (3.2) UIPickerViewDelegate
//设置单元格显示内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component ==0 ) {
return [picker_array objectAtIndex:row];
}
else{
NSString *string = [NSString stringWithFormat:@"%d月",row+indexN];
return string;
}
}
//设置选中的piker中的某一行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *get_str = [self pickerView:pickerView titleForRow:row forComponent:1];
NSLog(@"Com:%@",get_str);
if (component ==0 ) {
indexN = 3*row+1;
NSLog(@"indexn:%d",indexN);
[pickerView reloadAllComponents];
}
}
#pragma mark - 4.UIDatePicker时间选择器
- (IBAction)showDateView:(id)sender {
UIDatePicker *date_picker = [[UIDatePicker alloc]initWithFrame:CGRectMake(5, 100, 260, 200)];
date_picker.date = [NSDate date];
date_picker.tag = 1007;
//显示样式
date_picker.datePickerMode = UIDatePickerModeDate;
[self.view addSubview:date_picker];
}
- (IBAction)doShowTime:(id)sender {//选择按钮
UIDatePicker *datePK = (UIDatePicker*)[self.view viewWithTag:1007];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"YYYY-MM-dd hh:mm:ss aaa";
_showL.text = [formatter stringFromDate:datePK.date];
}
@end