gpt4 book ai didi

ios - uidatepicker [__NSDate 日期] : unrecognized selector sent to instance

转载 作者:行者123 更新时间:2023-11-28 22:46:48 29 4
gpt4 key购买 nike

我正在使用 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 期间不可用并且抛出异常。选择按钮的行为也相同,这让我怀疑是否还有其他问题。

最佳答案

viewDidLoad 中,这一行:

self.timePicker  = [NSDate date];

NSDate 对象分配给 timePicker(声明为 UIDatePicker 对象)。

稍后,代码尝试通过调用 date 实例方法获取日期选择器的当前日期值,并且(幸运的是)失败并返回 unrecognized selector 因为 NSDate(timePicker 现在指向的)没有这样的实例方法。


你在 viewDidLoad 中真正想做的是:

self.timePicker.date  = [NSDate date];

timePicker 内设置 date 属性。

关于ios - uidatepicker [__NSDate 日期] : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003623/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com