左右点击--日期增减

-(UIView *)viewGetByDate

{

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(20, 20, 180, 50)];

    //view.backgroundColor=[UIColor redColor];

    UIButton *btnRight=[UIButton buttonWithType:UIButtonTypeCustom];

    btnRight.frame=CGRectMake(view.frame.size.width-30, 10, 20, 20);

    btnRight.tag=160;

    [btnRight setBackgroundImage:[UIImage imageNamed:@"right1"] forState:UIControlStateNormal];

    [btnRight addTarget:self action:@selector(btnDateClick:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:btnRight];

    

    UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom];

    btnLeft.frame=CGRectMake(10, 10, 20, 20);

    btnLeft.tag=161;

    [btnLeft setBackgroundImage:[UIImage imageNamed:@"left1"] forState:UIControlStateNormal];

    [btnLeft addTarget:self action:@selector(btnDateClick:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:btnLeft];

    

    _labTime=[[UILabel alloc]initWithFrame:CGRectMake(35, 10, view.frame.size.width-60, 20)];

    _labTime.text=@"2015-12-28";

    _labTime.textAlignment=NSTextAlignmentCenter;

    [view addSubview:_labTime];

    return view;

}

-(void)btnDateClick:(UIButton *)sender

{

    //2012-12-10

    NSInteger year=[[_labTime.text substringWithRange:NSMakeRange(0, 4)] integerValue];

    NSInteger mouth=[[_labTime.text substringWithRange:NSMakeRange(5, 2)] integerValue];

    NSInteger day=[[_labTime.text substringWithRange:NSMakeRange(8, 2)] integerValue];

    if (sender.tag==161)

    {

        if (day>1) {

            day--;

        }

        else

        {

            if (mouth>1) {

                mouth--;

                day=[MyTime getDayCountOfMouthAndYear_mouth:mouth year:year];

            }

            else

            {

                mouth=12;

                day=[MyTime getDayCountOfMouthAndYear_mouth:mouth year:year];

                year--;

            }

 

        }

    }

    

    if (sender.tag==160)

    {

        NSInteger count=[MyTime getDayCountOfMouthAndYear_mouth:mouth year:year];

        if (day<count) {

            day++;

            

        }

        else

        {

            if (mouth<12) {

                mouth++;

                day=1;

            }

            else

            {

                mouth=1;

                day=1;

                year++;

            }

            

        }

    }

    NSString *d=[NSString stringWithFormat:@"%d",day];

    NSString *y=[NSString stringWithFormat:@"%d",year];

    NSString *m=[NSString stringWithFormat:@"%d",mouth];

    if (day<10) {

        d=[NSString stringWithFormat:@"0%d",day];;

    }

    if (mouth<10) {

        m=[NSString stringWithFormat:@"0%d",mouth];;

    }

    if (year<1000) {

        y=[NSString stringWithFormat:@"0%d",year];;

    }

    _labTime.text=[NSString stringWithFormat:@"%@-%@-%@",y,m,d];

}

原文地址:https://www.cnblogs.com/lvchenhao/p/4451746.html