iOS 时钟分选择器

#import "UIView+WZP_frame.h"

#import <UIKit/UIKit.h>

@protocol PickDelegate <NSObject>

@optional

-(void)selectHour:(NSInteger)ho mintue:(NSInteger)min btnTag:(NSUInteger)tag;

@end

@interface HourAndMin : UIView

@property (nonatomic,strong)UILabel *selectTime;

@property (nonatomic,strong)id<PickDelegate>delegate;


#import "HourAndMin.h"

@interface HourAndMin ()<UIPickerViewDelegate,UIPickerViewDataSource>

{


    NSInteger hourIndex;

    NSInteger minIndex;

}

@property(nonatomic,strong)NSMutableArray *proTimeList;

@property(nonatomic,strong)NSMutableArray *proTitleList;

@property(nonatomic,strong)UILabel *hourLabel;

@property(nonatomic,strong)UILabel *minLabel;

@end

@implementation HourAndMin


- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        _selectTime.text=@"00:00";

        _proTimeList=[NSMutableArray array];

        _proTitleList=[NSMutableArray array];

        [self addPickView];

    }

    return self;

}

-(void)addPickView{

    UIButton *btn0=[[UIButton alloc]initWithFrame:CGRectMake(0, self.wzp_height*0.4, 60, 40)]

    ;

    btn0.tag=0;

    [btn0 setTitle:@"取消" forState:UIControlStateNormal];

    [btn0 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

    [btn0 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(self.wzp_width-60, self.wzp_height*0.4, 60, 40)]

    ;

    btn1.tag=1;

    [btn1 setTitle:@"确认" forState:UIControlStateNormal];

    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

    [btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];


    

    UIPickerView *pickerView = [[UIPickerView alloc] init];

    self.frame=self.bounds;


    // 显示选中框

    pickerView.showsSelectionIndicator=YES;

    pickerView.dataSource = self;

    pickerView.delegate = self;

  

    [self addSubview:pickerView];

      [self addSubview:btn0];

       [self addSubview:btn1];

    for (int i=0; i<60; i++) {

        NSString *str=[NSString stringWithFormat:@"%02d",i];

        [_proTimeList addObject:str];

    }

    for (int i=0; i<24; i++) {

        NSString *str=[NSString stringWithFormat:@"%02d",i];

        [_proTitleList addObject:str];

    }

}

-(void)click:(UIButton*)btn{

    

    if ([_delegate respondsToSelector:@selector(selectHour:mintue:btnTag:)]) {

        NSLog(@"index---%02ld",hourIndex);

        NSLog(@"index---%02ld",minIndex);

        [_delegate selectHour:hourIndex mintue:minIndex btnTag:btn.tag];

    }

}

#pragma Mark -- UIPickerViewDataSource

// pickerView 列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 2;

}


// pickerView 每列个数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if (component == 0) {

        return [_proTitleList count];

    }

    

    return [_proTimeList count];

}

#pragma Mark -- UIPickerViewDelegate

// 每列宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

    

    if (component == 1) {

        return 80;

    }

    return 180;

}

// 返回选中的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (component == 0) {

      

        hourIndex=row;

        

    } else {

        minIndex=row;

    }

    

}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    if (component == 0) {

        return [_proTitleList objectAtIndex:row];

    } else {

        return [_proTimeList objectAtIndex:row];

    }

}


用法:


#import "ViewController.h"

#import "HourAndMin.h"

#import "UIView+WZP_frame.h"


@interface ViewController ()<PickDelegate>

{

    HourAndMin *hour;

}

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self addhourAndMin];

    

}

-(void)addhourAndMin{

    hour=[[HourAndMin alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.4)];

    hour.delegate=self;

    hour.center=self.view.center;

    [self.view addSubview:hour];

   

}

#pragma mark PickDelegate

-(void)selectHour:(NSInteger)ho mintue:(NSInteger)min btnTag:(NSUInteger)tag{

    if (tag==0) {

        [hour removeFromSuperview];

    }else{

        self.timeLabel.text=[NSString stringWithFormat:@"%02ld:%02ld",ho,min];

         [hour removeFromSuperview];

    }

}

- (IBAction)add:(id)sender {

    [self addhourAndMin];

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值