views:

45

answers:

1

i have a view which has a datepicker and a button added to it. There is another view which adds the above view as subview. But the events like touchesBegan and button's action are not being clicked on the subview. Please help

The code of the parent view is:

iTagDatePicker *dt=[[iTagDatePicker alloc] initWithFrame:CGRectMake(0.0, 180.0, 320.0, 240.0)];
    //dt.userInteractionEnabled=YES;
    //[dt becomeFirstResponder];

    dt.backgroundColor=[UIColor clearColor];
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.0];
    CGPoint cntr;
    cntr.x=160.0;
    cntr.y=420.0;
    dt.center=cntr;
    [self.view addSubview:dt];
    self.view.userInteractionEnabled=YES;
    CGPoint cntr1;
    cntr1.x=160.0;
    cntr1.y=158.0;
    dt.center=cntr1;
    [UIView commitAnimations];
    [dt release];

and the code for the sub class is:

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        dtPicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, 320.0, 216.0)];
        dtPicker.datePickerMode=UIDatePickerModeDate;
        dtPicker.date=[NSDate date];
        [self addSubview:dtPicker];

        btn=[[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
        btn.frame=CGRectMake(110.0, 400.0, 100.0, 20.0);
        [btn setTitle:@"Done" forState:UIControlStateNormal];
        btn.userInteractionEnabled=YES;
        [btn becomeFirstResponder];
        [btn addTarget:self action:@selector(SelectedVal:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:btn];


    }
    return self;
}

The button is not working

A: 

Subclass the parent view and pass touches to its children views.

Alex Reynolds
Hi. i have added the code. Could you elaborate what wrong am doing?
Deepika
I don't know. But if you want to do this properly, subclass the parent `UIView`, add properties for the `UIButton` and date picker objects, and then pass touches to those properties.
Alex Reynolds