Adding Date Picker to view programmatically?

梦想的初衷 提交于 2019-12-22 01:38:22

问题


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


回答1:


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];
    [self.view addSubview:myPicker];
    [myPicker release]; 
}

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



回答2:


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];



回答3:


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



来源:https://stackoverflow.com/questions/407460/adding-date-picker-to-view-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!