Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to get UIPickerView to display in second view controller?

declan0872declan0872 IrelandPosts: 3New Users Noob
Hi all,

I'm new to iOS programming so bear with me.. :P

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.

Anyone have any ideas?
And any help would be greatly appreciated!


HeightPickerViewController.h
#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
@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

Replies

  • ryantcbryantcb Posts: 667Registered Users @ @ @
    Just put another picker in the RegisterViewController. Or did mean you wanted the values returned into RegisterViewController?
  • UIUsUIUs Posts: 169Moderators Admin
    Please remove the unneeded blank lines in your code. It is difficult to read your code one - three lines at a time by scrolling.
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2015
    Short answer: Don't. You should treat a view controller's views as private.

    You can host another view controller as a child inside your view controller, but for something like a picker you'd be better off just putting a picker in both.

    You can create a common base class for both view controllers that implements your picker functionality if it's the same in multiple places. (Call it something like PickerHostingViewController.) That can get messy if you have multiple custom view controller classes however. Another option is a category that adds methods to the UIViewController class.

    P.S.: Your post has way, waaaaaay too much white space to be readable. As UIUs says, please edit it to remove all that extra whitespace.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.