我有UIViewController
與UIDatePicker
裏面。我在頭類IBOutlet
屬性連接UIDatePicker
,因爲我用我的應用程序的故事板:UIDatePicker不更改值
#import <UIKit/UIKit.h>
@protocol CreationDateDelegate <NSObject>
@optional
-(void)selectedDateIs:(NSString*)dateValue;
@end
@interface SelectCreationDateViewController : UIViewController
{
IBOutlet UIDatePicker *datePicker;
}
@property (nonatomic,assign)id<CreationDateDelegate>creationDateDelegate;
@property (nonatomic,retain) IBOutlet UIDatePicker *datePicker;
@end
--- .M -----
#import "SelectCreationDateViewController.h"
@interface SelectCreationDateViewController()
{
NSString *dateFromString;
}
@end
@implementation SelectCreationDateViewController
@synthesize creationDateDelegate,datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)viewDidLoad
{
self.title = @"Select creation date";
[super viewDidLoad];
[datePicker setHidden:NO];
datePicker.timeZone = [NSTimeZone localTimeZone];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker addTarget:self action:@selector(changeValueOfDatePicker:) forControlEvents:UIControlEventValueChanged];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillDisappear:(BOOL)animated{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSString *dateFromString = [formatter stringFromDate:datePicker.date];
[self.creationDateDelegate selectedDateIs:dateFromString];
}
-(void)changeValueOfDatePicker:(id)sender{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
dateFromString = [formatter stringFromDate:datePicker.date];
[self.creationDateDelegate selectedDateIs:dateFromString];
NSLog(@"Date from string value equal %@",dateFromString);
}
@end
爲什麼每一次當我改變DatePicker返回值的值不會更改?有任何想法嗎?另外,讓我知道如果我在代碼中犯了任何錯誤。
您的代碼changeValueOfDatePicker:方法在哪裏? –
其中聲明瞭UIDatePicker的代表 – codercat
@iDev沒有UIDatePicker的代理。 –