Search code examples
iphoneobjective-cxcodeuipickerview

Objective C getting pickerview values


another Objective-C one for you, probably pretty obvious but I have been at this for a few days now, and can't get it to work after trawling through similar problems on here and elsewhere!

Pretty much I have a 5 segment picker which, when a button is clicked, an alert sheet is shown which once accepted grabs the values of the picker wheels. Easy huh, yet I cant get it to work and I'm not sure where I'm going wrong... Some code below... it's probably pretty bad... but it's all pretty new to me so any help would be appreciated. All this is done without using InterfaceBuilder by the way.

Everything up to the loop after the action sheet works fine... I just can't get the values out!

(ps, i hope the formatting works! And I have changed some of my namings and that but all should be right)

Thanks!

MyFile.h

@interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UIActionSheetDelegate>
{
    UIPickerView * thePickerView;
    NSArray *thePickerValues;
}
@property (nonatomic, retain) UIPickerView *thePickerView;
@property (nonatomic, retain) NSArray *thePickerValues;

- (void)buildPicker;
- (void)doAction;

@end

MyFile.m

@implementation MyViewController
@synthesize thePickerView;
@synthesize thePickerValues;

- (void)viewDidLoad
{
    NSArray *values = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
    thePickerValues = values;
    [values release];
    [self buildPicker];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 5;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.thePickerValues count];
}

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

-(void)buildPicker
{
    CGRect pickerFrame = CGRectMake(0.0f,130.0f,320.0,100.0f);
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    [pickerView release];
}



- (void)doAction
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:[NSString stringWithFormat:@"Blah blah"]
                                  delegate:self
                                  cancelButtonTitle:@"No!"
                                  destructiveButtonTitle:@"Yes!"
                                  otherButtonTitles:nil];
    [actionSheet showInView:self.parentViewController.tabBarController.view];
    [actionSheet release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(!(buttonIndex == [actionSheet cancelButtonIndex]))
    {
        NSLog(@"Get the picker values");
        for(int i = 0; i<5;i++)
        {
            NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];
            NSLog(@"Wheel : %i Value : %i", i, selectedValue);
            [selectedValue release];
        }
    }
}

Solution

  • An alternative answer: thePickerView is nil. I don't see you assigning a value to it, right?