请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - 如何更改标签上的日期选择器选定值

[复制链接]
菜鸟教程小白 发表于 2022-12-13 17:23:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想在标签上显示从 datepicker 中选择的日期,但标签只显示当前日期它不显示我从 datepicker 中选择的日期这是代码请告诉我如何更改文本标签从日期选择器中随机选择的日期

#import "ViewController.h"

@implementation ViewController
@synthesize datepicker,mylabel;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDate *chosen = datepicker.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateStyle = NSDateFormatterMediumStyle;
    mylabel.text = [formatter stringFromDate:chosen];
     [formatter release];    

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppearBOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppearBOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappearBOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappearBOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end



Best Answer-推荐答案


    // code for Date picker for current date

     datepicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 320, 150)];
        //[self.view addSubview:datepicker];
        datepicker.date = [NSDate date];
        datepicker.datePickerMode=UIDatePickerModeDate;
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.timeStyle = NSDateFormatterNoStyle;

        label.text=[NSString stringWithFormat"%@",
                   [df stringFromDate:[NSDate date]]];
        df.dateFormat = @"yyyy-MM-dd";

    // set action for date picker

       [datepicker addTarget:self  actionselector(DateChange
            forControlEvents:UIControlEventValueChanged];

    //method for change date

    - (void)DateChangeid)sender
{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat"dd-MM-yyyy"];
        label.text = [dateFormatter stringFromDate:datepicker.date];
        date.text=strDate;
}

关于objective-c - 如何更改标签上的日期选择器选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044724/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap