How to get UIPickerView to display in second view controller?

Discussion in 'iOS Programming' started by declan0872, Apr 4, 2015.

  1. declan0872, Apr 4, 2015
    Last edited by a moderator: Apr 6, 2015

    declan0872 macrumors newbie

    Joined:
    Apr 4, 2015
    #1
    Hi all,

    I have two view controllers. RegisterViewController and HeightPickerViewController. I have created a picker view in HeightPickerViewController and I want to display this in RegisterViewController but dont know how to.

    HeightPickerViewController.h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface HeightPickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
    
    
    @property (strong, nonatomic) NSMutableArray *feetOptions;
    @property (strong, nonatomic) NSMutableArray *inchesOptions;
    @property (strong, nonatomic) UIPickerView *heightPickerView;
    
    @end
    
    HeightPickerViewController.m
    Code:
    @implementation HeightPickerViewController
    
    @synthesize feetOptions, inchesOptions, heightPickerView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        heightPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 200)];
        heightPickerView.delegate = self;
        heightPickerView.dataSource = self;
        [self.view addSubview:heightPickerView];
    }
    
    //RETURN THE NUMBER OF COLUMNS IN PICKER VIEW
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 4;
    }
    
    //RETURNS THE NUMBER OF ROWS IN EACH COMPONENT..
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        if(component == 0)
        {
            return [[self getUserDetailsHeightFeetOptions]count];
        }
        else
        {
            return [[self getUserDetailsHeightInchesOptions]count];
        }
        
    }
    
    //ADD DATA TO PICKER
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        if(component == 0)
        {
            return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightFeetOptions]objectAtIndex:row]];
        }
        else if(component == 1)
        {
            return [NSString stringWithFormat:@"Ft"];
        }
        else if (component == 2)
        {
            return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightInchesOptions]objectAtIndex:row]];
        }
        
        return [NSString stringWithFormat:@"In"];
        
        
    }
    
    - (NSArray*)getUserDetailsHeightFeetOptions
    {
        feetOptions = [[NSMutableArray alloc]init];
        for (int i = 2; i < 8; i++) {
            [feetOptions addObject:[NSNumber numberWithInt:i]];
        }
        return feetOptions;
       
    }
    
    - (NSArray*)getUserDetailsHeightInchesOptions
    {
        inchesOptions = [[NSMutableArray alloc]init];
        for (int i = 0; i < 12; i++) {
            [inchesOptions addObject:[NSNumber numberWithInt:i]];
        }
        return inchesOptions;
    
    }
    
    //WHAT HAPPENS WHEN A ROW IS PICKED
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    @end
    
    
    
     
  2. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    Do you want to have two separate views/screens? Or do you simply want to display a UIPickerView in the RegisterViewController?
     

Share This Page