rich
rich

Reputation: 1226

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];
        }
    }
}

Upvotes: 0

Views: 5823

Answers (4)

Steven Kramer
Steven Kramer

Reputation: 8513

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

Upvotes: 1

benzado
benzado

Reputation: 84308

As others have pointed out, NSInteger is not an object.

NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];

What is happening in this line is that selectedRowInComponent: is returning a number indicating the index of the selected row. Because you are assigning it to an NSInteger pointer instead, the computer is using the value as a memory address. (When you declare Whatever *foo that means that foo contains a memory address, and *foo is the value at that address.)

If you tried to dereference (get the value pointed to by) selectedValue, your app will likely crash. Or worse, not crash, and return whatever junk is at that arbitrary address.

More importantly, you are mixing up what the method returns (the index) with what you want (the value).

Here's what you need:

NSInteger selectedRow = [thePickerView selectedRowInComponent:i];
NSString *selectedValue = [thePickerValues objectAtIndex:selectedRow];

Now you've got what you want in selectedValue, and can put it in your action sheet.

Upvotes: 0

Stephen Darlington
Stephen Darlington

Reputation: 52565

In this line:

NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];

You should be aware that an NSInteger is just a name for an int (actually, the underlying type depends on the OS but it's always a concrete type and not an Objective C object). Or put another way, it's not a pointer. Try:

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];

I'm surprised you didn't get a compiler warning about this.

Upvotes: 1

Steven Kramer
Steven Kramer

Reputation: 8513

An NSInteger is not an object. It's plain old data. Look up its definition now. Go on, command-double click the symbol in your source file. Or option-double click to look it up in the reference manual.

And this should do it.

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];
NSLog(@"Wheel : %i Value : %i", i, selectedValue);
// [selectedValue release];

Upvotes: 0

Related Questions