Monday, November 15, 2010

uidatepicker+backgroundcolor

Hi All,
I think I understand how to put a UIDatePicker on a new UIView and let my users hit a button to bring that view up... but its kinda slow and doesn't look slick. I would like to have my users hit a button and the UIDatePicker appear from the bottom of the screen as a subview (like the keyboard).

I did some searching in this forum and jonc asked a similar question a while back. I tried to work from that thread but I just can't get things working.

Could some kind soul post the code to do this type of thing. One note, I am a couple of screens down into the my app when I need to get this date, so I'm not executing in the appDelegate code.

jonc said to use:
myProgramAppDelegate *appDelegate = (myProgramAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:datePicker];

and this somewhat makes sense but I need to see more of the code around it to get the whole picture - like creating the datePicker view and getting the value back out once something has been selected. Also, what if you bring up this subview and then don't want to change the date. Is there a way to cancel (i.e. click somewhere else on your screen)?

Thanks for the help and your patience as the java guy continues to struggle with iPhone dev. 







I've made some progress, but I'd like to know if the group thinks this is the best way to do this:

- (IBAction) updateDueDateid) sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// create a new UIDatePicker and show as a subview
// is this the first time to navigate to this screen?
if (dueDatePickerView == nil) {
dueDatePickerView = [[UIDatePicker alloc] initWithFrame:CGRectZero];
dueDatePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
dueDatePickerView.datePickerMode = UIDatePickerModeDate;

CGSize pickerSize = [dueDatePickerView sizeThatFits:CGSizeZero];
dueDatePickerView.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
dueDatePickerView.backgroundColor = [UIColor blackColor];

[dueDatePickerView addTarget:self action:@selector(dueDateChangedforControlEvents:UIControlEventValueChanged];
dueDatePickerView.hidden = NO;
[appDelegate.window addSubview:dueDatePickerView];
} else {
dueDatePickerView.hidden = NO;
}

// create the dismiss button
if (closeDueDatePickerButton == nil) {
closeDueDatePickerButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
closeDueDatePickerButton.frame = CGRectMake(100, 75, 120, 50);
[closeDueDatePickerButton setTitle:@"Dismiss" forState:UIControlStateNormal];
[closeDueDatePickerButton setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
closeDueDatePickerButton.backgroundColor = [UIColor blackColor];
[closeDueDatePickerButton addTarget:self action:@selector(closeDueDateAction) forControlEvents:UIControlEventTouchUpInside];
closeDueDatePickerButton.hidden = NO;
[self.view addSubview:closeDueDatePickerButton];
} else {
closeDueDatePickerButton.hidden = NO;
}
}

-(void) dueDateChangedid)sender {
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

taskDueDate.text = [dateFormatter stringFromDate:[dueDatePickerView date]];
//NSLog(@"changed to %@", taskDueDate.text);
}

- (void)closeDueDateAction {
// dismiss the UIDatePicker
dueDatePickerView.hidden = YES;
closeDueDatePickerButton.hidden = YES;
}

I've created this view using an XIB file. I suppose I could have put the UIDatePicker and UIButton on that view and then just hidden/show them when I need to.

Thoughts? And are they better ways to do this?

Thanks,
Stalin




No comments:

Post a Comment

Followers