views:

3061

answers:

3

Can someone illustrate (or point me to a good tutorial) on how to add a Date Picker to a view programatically (i.e., without using the interface editor)?

+1  A: 

Create it as you would a normal object, using UIView's initWithFrame method. Keep in mind that frame rectangle you specify should be created in terms of the superview's bounds. Here's an example, you'll probably need to change the frame rectangle, and add your own code to set up the date picker as needed.

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:[superview bounds]];
[superview addSubview:datePicker];
[datePicker release];
Marc Charbonneau
A: 

Try the UIControls example provided in the iPhone SDK sample code. That shows you how to add almost any control programatically.

And the size of a picker is fixed. It is 320x216

lostInTransit
A: 

It's very easy man.You can do it in seconds ,

Just use the these methods in your.m file...all is ready

  • (void)viewDidLoad {

     CGRect pickerFrame = CGRectMake(0,250,0,0);
    

    UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];

    [scrollView addSubview:myPicker];

    [myPicker release]; }

  • (void)pickerChanged:(id)sender { NSLog(@"value: %@",[sender date]); }

Ajay Sharma