0

So i found this amazing code on here that pops up a UIDatePicker. (kudos to Matthias Bauch) I'd like to customize it with a custom picker wheel, but the code is at a complexity level that I'm not sure how to go about switching to a UIPicker and how to fill said picker with my arrays.

Here is the original code:

- (void)changeDate:(UIDatePicker *)sender {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  [dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];
  NSString *formattedVersion = [dateFormat stringFromDate:sender.date];
  NSLog(@"%@",formattedVersion);

}

- (void)removeViews:(id)object {
 [[self.view viewWithTag:9] removeFromSuperview];
 [[self.view viewWithTag:10] removeFromSuperview];
 [[self.view viewWithTag:11] removeFromSuperview];
}

- (void)dismissDatePicker:(id)sender {
 CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
 CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
 [UIView beginAnimations:@"MoveOut" context:nil];
 [self.view viewWithTag:9].alpha = 0;
 [self.view viewWithTag:10].frame = datePickerTargetFrame;
 [self.view viewWithTag:11].frame = toolbarTargetFrame;
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(removeViews:)];
 [UIView commitAnimations];
}

- (IBAction)callDP:(id)sender {
 if ([self.view viewWithTag:9]) {
  return;
 }
 CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
 CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

 UIView *darkView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
 darkView.alpha = 0;
 darkView.backgroundColor = [UIColor blackColor];
 darkView.tag = 9;
 UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] autorelease];
 [darkView addGestureRecognizer:tapGesture];
 [self.view addSubview:darkView];

 UIDatePicker *datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] autorelease];
 datePicker.tag = 10;
 [datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
 [self.view addSubview:datePicker];

 UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] autorelease];
 toolBar.tag = 11;
 toolBar.barStyle = UIBarStyleBlackTranslucent;
 UIBarButtonItem *spacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
 UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] autorelease];
 [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
 [self.view addSubview:toolBar];

 [UIView beginAnimations:@"MoveIn" context:nil];
 toolBar.frame = toolbarTargetFrame;
 datePicker.frame = datePickerTargetFrame;
 darkView.alpha = 0.5;
 [UIView commitAnimations];
}

I'd like to add my UIPicker with these values in the picker wheel:

  Days=[[NSArray alloc] initWithObjects: @"Today", @"Tomorrow", nil];
  Hours = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil];
  Minutes=[[NSArray alloc] initWithObjects:@"05", @"10", @"15", @"20", @"25", @"30", @"35", @"40", @"45", @"50", @"55", nil];

Any help/guidance would be really appreciated!!

Thanks, Mat

mathewwl
  • 49
  • 7

2 Answers2

0

You will need to use a UIPickerView instead of a UIDatePicker. You will then need to supply the proper delegate methods to provide the data for it.

First create an array to hold items that will be available in the picker:

NSMutableArray *arrayColors = [[NSMutableArray alloc] init];
[arrayColors addObject:@"Red"];
[arrayColors addObject:@"Orange"];

Implement data source methods for picker:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

return [arrayColors count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}

And the delegate method to get what item the user selected:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}

Here is the documentation.

Update

To create a UIPickerView programmatically make sure it looks something like this:

//Make sure our class knows we have picker delegate/data source methods in it
@interface myViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

The creation

//Picker creation
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

A handy delegate method

// tell the picker the width of each row for a given component. no necessary but helpful if need be
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;

 return sectionWidth;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
random
  • 8,568
  • 12
  • 50
  • 85
  • My problem isn't so much with creating a pickerView (although thank you for that!). I'm more wondering how to incorporate that with the code that I already have. – mathewwl Apr 04 '13 at 02:31
  • Change UIDatePicker to UIPickerView – random Apr 04 '13 at 02:33
  • I've tried that and all that pops up is an empty darkView. Any ideas? – mathewwl Apr 04 '13 at 02:42
  • Did you implement all of the delegate methods? It sounds like it doesn't know what data to display so it is just showing it as blank. – random Apr 04 '13 at 02:46
  • I just copied and pasted your code in and changed to **UIPickerView *TimePicker_ = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];** – mathewwl Apr 04 '13 at 02:52
  • Did you update the numberOfComponentsInPickerview and numberOfRowsInComponent to reflect your data arrays? – random Apr 04 '13 at 02:56
  • Yep, i did! Apologies, I guess i did do a little more than copy and paste. – mathewwl Apr 04 '13 at 02:58
0

Adding off random's comment. If you would like to use this picker to replace your keyboard when pressing a UITextField to input information to the textfield here's how.

[<Your UI text field> setInputView:<your picker view>];

example:

NSMutableArray *pickerData = [[NSMutableArray alloc]initWithObjects:@"",@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
UIPickerView *myPickerView = [[UIPickerView alloc]init];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.myTexfield setInputView:myPickerView];
Blancotech
  • 145
  • 6
  • Still not quite what I'm looking for. My main issue isn't creating a new UIPickerView, it's incorporating said pickerView into my existing code. – mathewwl Apr 04 '13 at 02:35