iOS 时钟分选择器

来源:互联网 发布:mc建筑装饰 知乎 编辑:程序博客网 时间:2024/05/16 09:19

#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 = [superinitWithFrame:frame]) {

        _selectTime.text=@"00:00";

        _proTimeList=[NSMutableArrayarray];

        _proTitleList=[NSMutableArrayarray];

        [selfaddPickView];

    }

    returnself;

}

-(void)addPickView{

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

    ;

    btn0.tag=0;

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

    [btn0 setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    [btn0 setTitleColor:[UIColorblackColor] forState:UIControlStateHighlighted];

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

    

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

    ;

    btn1.tag=1;

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

    [btn1 setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    [btn1 setTitleColor:[UIColorblackColor] forState:UIControlStateHighlighted];

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


    

    UIPickerView *pickerView = [[UIPickerViewalloc] init];

    self.frame=self.bounds;


    // 显示选中框

    pickerView.showsSelectionIndicator=YES;

    pickerView.dataSource =self;

    pickerView.delegate =self;

  

    [selfaddSubview:pickerView];

      [selfaddSubview:btn0];

       [selfaddSubview:btn1];

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

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

        [_proTimeListaddObject:str];

    }

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

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

        [_proTitleListaddObject:str];

    }

}

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

    

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

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

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

        [_delegateselectHour:hourIndexmintue:minIndexbtnTag:btn.tag];

    }

}

#pragma Mark -- UIPickerViewDataSource

// pickerView 列数

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

    return2;

}


// pickerView 每列个数

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

    if (component ==0) {

        return [_proTitleListcount];

    }

    

    return [_proTimeListcount];

}

#pragma Mark -- UIPickerViewDelegate

// 每列宽度

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

    

    if (component ==1) {

        return80;

    }

    return180;

}

// 返回选中的行

- (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 [_proTitleListobjectAtIndex:row];

    } else {

        return [_proTimeListobjectAtIndex:row];

    }

}


用法:


#import "ViewController.h"

#import "HourAndMin.h"

#import "UIView+WZP_frame.h"


@interface ViewController ()<PickDelegate>

{

    HourAndMin *hour;

}

@property (weak,nonatomic) IBOutletUILabel *timeLabel;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfaddhourAndMin];

    

}

-(void)addhourAndMin{

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

    hour.delegate=self;

    hour.center=self.view.center;

    [self.viewaddSubview:hour];

   

}

#pragma mark PickDelegate

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

    if (tag==0) {

        [hourremoveFromSuperview];

    }else{

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

         [hourremoveFromSuperview];

    }

}

- (IBAction)add:(id)sender {

    [selfaddhourAndMin];

}



1 0
原创粉丝点击