16

有人可以说明(或指向我一个很好的教程)如何以编程方式将日期选择器添加到视图中(即,不使用界面编辑器)?

4

3 回答 3

39

这很容易,你可以在几秒钟内完成。

只需在 your.m 文件中使用这些方法...一切就绪

- (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]);
}
于 2010-10-14T08:31:54.467 回答
3

initWithFrame使用 UIView 的方法像创建普通对象一样创建它。请记住,您指定的框架矩形应该根据超级视图的边界创建。这是一个示例,您可能需要更改框架矩形,并根据需要添加您自己的代码来设置日期选择器。

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:[superview bounds]];
[superview addSubview:datePicker];
[datePicker release];
于 2009-01-02T17:27:17.520 回答
2

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

于 2009-01-03T06:05:17.367 回答