1

我有两个视图的简单 xib:带有日期选择器的容器视图和带有条形按钮的工具栏。我将它用作我的inputView输入字段( )。但是我遇到了性能问题:我注意到加载这个 xib 文件的 nib 并从 nib 实例化视图需要大约 500 毫秒(使用 Time Profiler 仪器工具计算)。因此,我没有将日期选择器添加到 xib,而是以编程方式创建它并使用 vuela!,实例化只需要大约 30 毫秒。代码示例非常简单:inputAccessoryView<UIKeyInput>

- (void)initialize
{
    NSString * className = NSStringFromClass(self.class);
    UINib * nib = [UINib loadCachedForKey:className];
    [classNib instantiateWithOwner:self
                           options:nil];    
    _contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _contentView.frame = self.bounds;
    [self addSubview:_contentView];
}

我的类别在哪里[UINib loadCachedForKey:],它的实现就像方法名称听起来一样简单,所以我认为在这里发布它是没用的。

我也有一个类似的 xib,但使用 UIPicker 并且它的实例化速度很快(约 70 毫秒)。以编程方式添加日期选择器确实对我有用,但问题是
我做错了什么还是正常行为

4

1 回答 1

0

最好不要在此使用 xib 文件,以编程方式使用添加日期选择器:如果对您有用,请尝试以下操作:

- (IBAction)btnClicked:(id)sender {
    UIDatePicker *datepicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
    datepicker.datePickerMode = UIDatePickerModeDate;
    datepicker.hidden = NO;
    datepicker.date = [NSDate date];

    [datepicker addTarget:self
                   action:@selector(LabelChange:)
         forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datepicker]; //this can set value of selected date to your label change according to your condition


    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"M-d-yyyy"]; // from here u can change format..
    _selectedDate.text=[df stringFromDate:datepicker.date];
}
- (void)LabelChange:(id)sender{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"M-d-yyyy"];
    _selectedDate.text = [NSString stringWithFormat:@"%@",
                          [df stringFromDate:datepicker.date]];
}
于 2015-03-30T10:59:39.950 回答