UIPickerview in iphone

2019-08-24 22:38发布

问题:

How to place UIPickerView programmatically in a subView with out using Interface Builder?

回答1:

Hey! U can make UIPickerView programmatically....

In- viewController.m file

- (void)viewDidLoad {
    [super viewDidLoad];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
    [self.view addSubview:pickerView];
    [pickerView setDelegate:self];
        [pickerView release];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return ; //give components here
}

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

    return ;   //give rows here
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {   return ;  // give titles }

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //Do the step here
}

And in your viewController.h class never forgot to do this

@interface viewController : UIViewController <UIPickerViewDelegate>

Hope this may help u.....for more help follow the link ..