简体   繁体   English

如何创建自定义UIDatePicker?

[英]How do you create a custom UIDatePicker?

I am creating a health and fitness app for iOS. 我正在为iOS创建健康和健身应用程序。 As part of the user registration they have to enter in their height. 作为用户注册的一部分,他们必须输入身高。 I am asking the user to do this by using a date picker but I am unsure as to how I would go about doing this. 我要求用户使用日期选择器来执行此操作,但是我不确定如何执行此操作。 Anyone have any general tips or links that could guide me in the right direction? 任何人都有任何常规技巧或链接可以指导我正确的方向吗?

Cheers! 干杯!

Like rmaddy has alluded to, you will need to use a UIPickerView . 就像rmaddy提到的那样,您将需要使用UIPickerView UIDatePicker is for selecting dates / times only. UIDatePicker仅用于选择日期/时间。 Here's a link to the apple documentation: 这是苹果文档的链接:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerView_Class/index.html https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerView_Class/index.html

And here's some tips to help get you started: 以下是一些帮助您入门的提示:

1) Create a property of type UIPickerView on your instance of UIViewController . 1)在您的UIViewController实例上创建UIPickerView类型的属性。 I'm calling it pickerView for the purpose of demonstration. 为了进行演示,我将其pickerView

2) Either alloc init the pickerView or link it to a pickerView on a storyboard / xib file. 2)要么的alloc初始化的pickerView或将其链接到一个pickerView上的故事板/ XIB文件。

3) Make sure you set the delegate of the pickerView to be your instance of UIViewController . 3)确保将pickerViewdelegate设置为UIViewController的实例。

4) Make sure your instance of UIViewController implements the following protocols: UIPickerViewDataSource and UIPickerViewDelegate by adding this following line of code next to the interface declaration for your UIViewController subclass: 4)通过在UIViewController子类的接口声明旁边添加以下代码行,确保您的UIViewController实例实现以下协议: UIPickerViewDataSourceUIPickerViewDelegate

<UIPickerViewDataSource, UIPickerViewDelegate>

5) Actually implement the required methods (as well as whatever optional) of UIPickerViewDataSource and UIPickerViewDelegate . 5)实际上实现UIPickerViewDataSourceUIPickerViewDelegate的必需方法(以及任何可选方法)。 This is how you will tell your pickerView how many rows and columns it has, as well as what text to populate said rows and columns with. 这样,您就可以告诉pickerView它有多少行和列,以及用什么文本填充所说的行和列。

Some sample code is shown below: 一些示例代码如下所示:

// MyViewController.h

@interface MyViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@end

// MyViewController.m

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pickerView.delegate = self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    // components are like columns. one column for feet and one for inches
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0){
        return [[self getUserDetailsHeightFeetOptions]count];
    } else {
        return [[self getUserDetailsHeightInchesOptions]count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(component == 0){
        return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightFeetOptions]objectAtIndex:row]];
    } else {
        return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightInchesOptions]objectAtIndex:row]];
    }
}

- (NSArray*)getUserDetailsHeightFeetOptions{
    NSMutableArray *feetOptions = [[NSMutableArray alloc]init];
    for (int i = 3; i < 8; i++) {
        [feetOptions addObject:[NSNumber numberWithInt:i]];
    }
    return feetOptions;
}

- (NSArray*)getUserDetailsHeightInchesOptions{
    NSMutableArray *inchesOptions = [[NSMutableArray alloc]init];
    for (int i = 0; i < 12; i++) {
        [inchesOptions addObject:[NSNumber numberWithInt:i]];
    }
    return inchesOptions;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM