当我尝试从日期选择器获取日期时,我正在使用 uidatepicker 并抛出异常。
.h 文件:
#import <UIKit/UIKit.h>
@protocol TimeDelegate <NSObject>
- (void)returnTime:(NSDate *)item;
@end
@interface TimeController : UIViewController <UIPickerViewDelegate>
{
UIDatePicker *timePicker;
}
@property (nonatomic, weak) id <TimeDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIDatePicker *timePicker;
@property (nonatomic, retain) NSDate *activityTime;
@end
.m 文件
#import "TimeController.h"
@implementation TimeController
@synthesize timePicker;
- (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.
self.timePicker = [NSDate date];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) viewDidDisappear:(BOOL)animated
{
NSLog(@"%@", self.activityTime);
//NSDate *itemToPassBack = [self.timePicker date];
[self.delegate returnTime:self.activityTime];
}
- (IBAction)SelectTime:(id)sender {
self.activityTime = [timePicker date];
}
@end
当我单击选择或导航返回时会发生错误。最初我认为日期选择器在 viewdiddisappear 期间不可用并引发异常。选择按钮的行为也相同,这让我怀疑是否还有其他问题。