Ali
Ali

Reputation: 11590

Event is not occuring after adding the UIDatePicker on UIActionSheet

Please review the code

  -(IBAction)showDateSheet{
dateSheet=[[UIActionSheet alloc]initWithTitle:@"Select Test Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
datePicker=[[UIDatePicker alloc]init];
[datePicker addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
dateSheet.delegate=self;
//[dateSheet setFrame:CGRectMake(0, 117, 320, 383)];
datePicker.datePickerMode=UIDatePickerModeDate;
CGRect pickerRect=datePicker.bounds;
pickerRect.origin.y=-100;
datePicker.bounds=pickerRect;
[dateSheet addSubview:datePicker];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0.0f, 0.0f, 320, 500)];

//[datePicker release];
//[dateSheet release];

   }

by using above code I am not able to click the cancel button , when i tap on cancel button nothing happens

Upvotes: 0

Views: 184

Answers (2)

NJones
NJones

Reputation: 27147

Your error is setting the bounds of datePicker like so:

CGRect pickerRect=datePicker.bounds;
pickerRect.origin.y=-100;
datePicker.bounds=pickerRect;

Essentially what you have done is shifted where the picker is drawn, but not where the picker's frame is. To see this more clearly set the picker's clipsToBounds property to YES and look at what you get. Even if you had not set clipsToBounds, you would not be able to interact with the section below.

What you actually want to do is set the frames origin down, not the bounds origin up, by a certain number of points. The working code would look something like this:

CGRect pickerRect=datePicker.frame;
pickerRect.origin.y+=110;
datePicker.frame=pickerRect;

Upvotes: 1

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

You need to add the button on Action sheet something like this...

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[dateSheet addSubview:closeButton];
[closeButton release];

this is to be done before calling [dateSheet showInView:self.view];

Then in the selector just write this..

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]

Upvotes: 0

Related Questions