自定义UIView无法为子视图指定值

时间:2013-01-06 16:24:00

标签: ios uiview textfield instance-variables

我有一个UIView子类,它与xib相关联。我不是在layoutSubviews方法中创建视图,而是仅在xib中创建视图。何时使用self.ivar_ivar令人困惑。我尝试设置textField的文本,该文本是subview子类的UIView。谁能帮我??我尝试从我的textfield设置或获取viewController的文字。但我不能成功。这是一个代码。

这是我的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];
}

这是我的代码,用于将视图添加到viewcontroller并将文本分配给textfield并尝试获取文本?但是我在设置和获取文本方面都没有成功。

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);

日志显示为null ..任何帮助将不胜感激。我附上了UIView的完整代码,只是为了理解我在做什么。

1 个答案:

答案 0 :(得分:0)

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

所以,有两种可能性:

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

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

  3. 关于self.ivar_ivar的使用,唯一的区别在于前者将为您处理引用计数;对于后者,你必须自己做。因此,在这种情况下,与您使用的是哪一个无关。问题在于它们的初始化,以便它们正确引用您的子视图。