1

我有一个UIView子类,它与一个xib. 我不是在layoutSubviews方法中创建视图,而只是在xib. 何时使用self.ivar和令人困惑_ivar。我试图为我的子类textField的 a设置文本。谁能帮我??我试图从我的. 但我不能成功。这是一个代码。subviewUIViewtextfieldviewController

这是我的UIView自定义类DateRangeView.m

#import "DateRangeView.h"

@implementation DateRangeView
@synthesize tfStartDate = _tfStartDate;
@synthesize tfEndDate = _tfEndDate;
@synthesize startDate = _startDate;
@synthesize endDate = _endDate;
@synthesize dateFormatter = _dateFormatter;
@synthesize datePicker = _datePicker;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DateRangeView" owner:self options:nil];
        [self addSubview:[topLevelObjects objectAtIndex:0]];
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [self baseInit];
    }
    return self;

}

- (void)baseInit {
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateStyle:NSDateFormatterMediumStyle];
    _dateFormatter = df;

    _startDate = [NSDate date];
    _endDate = [NSDate date];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 320, 216)];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    [_datePicker setDate:[NSDate date]];
    _tfStartDate.inputView = _datePicker;
    _tfEndDate.inputView = _datePicker;

    UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width, 44)]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonPressed:)];

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];

    [keyboardToolbar setItems:[[NSArray alloc] initWithObjects:cancelBtn, flexibleSpace, doneButton, nil]];

    [keyboardToolbar setTranslucent:YES];
    [keyboardToolbar setTintColor:[UIColor blackColor]];

    [_tfStartDate setInputAccessoryView:keyboardToolbar];
    [_tfEndDate setInputAccessoryView:keyboardToolbar];

    _tfStartDate.text = [_dateFormatter stringFromDate:_startDate];
    _tfEndDate.text = [_dateFormatter stringFromDate:_endDate];

}

- (void)doneButtonPressed:(id)sender {
    if ([_tfStartDate isFirstResponder]) {

        self.startDate = _datePicker.date;
        [_tfStartDate resignFirstResponder];

    }else if ([_tfEndDate isFirstResponder]) {

        self.endDate = _datePicker.date;
        [_tfEndDate resignFirstResponder];
    }
}

- (void)cancelButtonPressed:(id)sender {
    [_tfStartDate resignFirstResponder];
    [_tfEndDate resignFirstResponder];
}

- (void)setStartDate:(NSDate *)startDate {
    _startDate = startDate;
    _tfStartDate.text = [_dateFormatter stringFromDate:_startDate];
}

- (void)setEndDate:(NSDate *)endDate {
    _endDate = endDate;
    _tfEndDate.text = [_dateFormatter stringFromDate:_endDate];
}

这是我将视图添加到视图控制器并将文本分配给文本字段并尝试获取文本的代码?但我在设置和获取文本方面都没有成功。

rangeView = [[DateRangeView alloc]initWithFrame:CGRectMake(0, 0, 320, 150)];
[self.view addSubview:rangeView];
rangeView.startDate = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *startDateComponents = [calendar components:NSDayCalendarUnit fromDate:[NSDate date]];
[startDateComponents setDay:5];

NSDate *beginDate = [calendar dateByAddingComponents:startDateComponents toDate:[NSDate date] options:0];
rangeView.endDate = beginDate;

NSLog(@"start %@", rangeView.tfStartDate.text);
NSLog(@"end %@", rangeView.tfEndDate.text);

日志显示为空。任何帮助将不胜感激。我附上了我UIView唯一的完整代码,以了解我在做什么。

4

1 回答 1

0

在您的代码中,我看不到self.tfStartDateand的初始化self.tfEndDate

所以,有两种可能:

  1. 它们应该是 IBOutlets:然后,检查您的 nib 文件,它们是否也连接到相关视图;

  2. 它们没有在您的 xib 中定义:在这种情况下,只需将它们初始化为viewDidLoad.

至于self.ivarvs.的使用_ivar,唯一的区别是前者会为你处理引用计数;对于后者,你必须自己做。因此,在这种情况下,您使用的是哪一个并不重要。问题在于它们的初始化,以便它们正确引用您的子视图。

于 2013-01-06T17:53:57.317 回答