为什么我的UILabel没有随着datepicker的变化而更新?

时间:2013-03-16 20:58:49

标签: ios objective-c

#import "DatePickerViewController.h"
#import <Parse/Parse.h>

@interface DatePickerViewController ()


@end

@implementation DatePickerViewController

@synthesize dateLabel;
@synthesize pick;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    UIBarButtonItem *saveDate = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Save Date"
                                 style:UIBarButtonItemStyleDone
                                 target:self
                                 action:@selector(saveList:)];
    self.navigationItem.rightBarButtonItem = saveDate;
    pick = [[UIDatePicker alloc] init];
    [pick setFrame:CGRectMake(0,200,320,120)];
    [pick addTarget:self action:@selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];


}

-(void)saveList:(id)sender {
    // need to finish

}

-(IBAction)updateDateLabel {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    dateLabel.text = [NSString stringWithFormat:@"%@", [formatter stringFromDate:pick.date]];

}

2 个答案:

答案 0 :(得分:1)

您从选择器注册事件的方法是使用一个需要一个参数的选择器(@selector(updateDateLabel:)需要一个-updateDateLabel:(id)arg形式的方法),而您实现的方法不带参数({{1} })

当然,考虑到你已经从故事板上解压缩了你的选择器,所有这一切都没有实际意义。删除初始化代码并将IBAction连接到故事板中的选择器。

答案 1 :(得分:0)

-(IBAction)updateDateLabel {更改为-(IBAction)updateDateLabel:(id)sender {