views:

41

answers:

2

Hello,

I'm trying to add a UIPicker as a subview, but can't quite figure it out. I've added a second UIView control with a UIPickerview to my nib (NewWorkoutViewController.xib). My code is as follows:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
    return 1;
}


-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    return [list count];
}


-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [list objectAtIndex:row];
}


-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"Selected item: %@ index of selected item: %i", [list objectAtIndex:row], row);
}

-(void)viewDidLoad 
{
    [super viewDidLoad];
    counterInt = 0;

    list = [[NSMutableArray alloc] init];
    [list addObject:@"red"];
    [list addObject:@"blue"];
    [list addObject:@"yellow"];
    [list addObject:@"pink"];
}   

-(IBAction)routePicker
{   
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
    UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
    [myView addSubview:thePickerView];
    [self.view.superview addSubview:myView];  
}

My subview is not popping up as expected.

+2  A: 

It is not clear why do you add the picker in code if you've already added it in IB... Regarding the code you posted, try to add picker to controller's view, not to the superview (and do not forget to set its delegate):

-(IBAction)routePicker
{   
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
    UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
    thePickerView.delegate = self;
    [myView addSubview:thePickerView];
    [self.view addSubview:myView];  
}

Edit: You initialize you myView instance with a frame that is likely to be outside of the visible area (frame's origin y coordinate is 480), try to set it to 0:

// Bad
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
// Good?
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; 
Vladimir
Thanks Vladimir, I didn't realise I was adding them programmatically and via the Interface Builder. I've removed both the UIPickerView and UIView from IB.I've tried the code above (and set the delegate) but UIPickerView is still not popping up.
Stephen
Thanks again, that worked. Just one thing, the picker appears at the top of the screen, what value to I adjust to make it appear at the bottom.
Stephen
Its okay, got its sorted.UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 250, 320, 216)];
Stephen
change 2nd parameter in CGRectMake (that is y coordinate for frame origin). for example CGRectMake(0, CGRectGetMaxY(myView.frame)-216, 320, 216) or just CGRectMake(0, 264, 320, 216)
Vladimir
Vladimir, at the moment I using a custom button that makes the subview appear, how would I achieve the same effect with a UITextView ?
Stephen
Track some textview event in its delegate? It is not very clear what are you trying to do... May be it worth creating separate question for that?
Vladimir
I'll create a separate post. Thanks again for your help.
Stephen
A: 

Check out this example from Apple

Date Cell Picker

black2842