Search code examples
iphoneiosnsmutablearrayuipickerview

UIPickerView appears but doesn't show data


I've been all over the place for the last two days trying to find the answer to this, but I can't seem to get it. As far as I can tell, I have everything setup correctly, but it just doesn't want to work.

My app scans QRCode tags and saves them to a mutable array. I need to create a way so the user can remove some of the tag objects from the array, so I'm using a UIPickerView to display the list of tags numbers from which to select the tags to delete.

Here's what I have:

in the .h file

@interface ViewController : UIViewController
    UIPickerView *tagPickerView;
    NSMutableArray *tagPickerData;

@property (nonatomic, retain) IBOutlet UIPickerView *tagPickerView;
@property (nonatomic, retain) NSMutableArray *tagPickerData;

in the .m file

    @synthesize tagPickerView;
    @synthesize tagPickerData;

-(void) viewDidLoad{
        tagPickerData = [[NSMutableArray alloc]init];
        [tagPickerView setDelegate:self];
        [tagPickerView setDataSource:self];
    }

- (void) dealloc{
    [tagPickerView release];
    [tagPickerData release];
    [super dealloc];
}

- (void) imagePickerController: (UIImagePickerController*) reader
     didFinishPickingMediaWithInfo: (NSDictionary*) info
    {
    [tagPickerData addObject:tagString]; //tagString is the value returned from the QRCode reader
}

  #pragma mark -
#pragma mark tagPickerView Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)tagPickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)tagPickerView
numberOfRowsInComponent:(NSInteger)component{
    return [tagPickerData count];


}

#pragma mark tagPickerView Delegate Methods
-(NSString *)pickerView:(UIPickerView *)tagPickerView
            titleForRow:(NSInteger)row
           forComponent:(NSInteger)component{
    return [self.tagPickerData objectAtIndex:row];
}

(I think that's everything.)

In IB I have dataSource, delegate and tagPickerView set for File's Owner. The UIPickerView is hidden until the delete command is called (in this case, shaking the phone), and then it appears on top of everything.

What I am getting is a blank picker view.

I can verify using NSLog that the array is being populated each time the scan is performed, so I know it's not that I am loading an empty array. I can also verify that the Data Source methods are apparently being read, because I can change the number of components to 2 and it is reflected when the picker comes up -- two spinning wheels. But I don't know how to verify that the Delegate method is working.

I wondered if there's a way to populate the picker on command, like when the phone is shaken, instead of using the delegate... or would it even work that way?

The problem with all the reference materials is that it demonstrates how to use an array that is initialized with objects, i.e.

NSArray *array = [[NSArray alloc] initWithObjects:@"String 1",@"string 2",@string 3",(etc.) ,nil];

But I can't seem to find anything that shows how to load the picker from an array created from information collected by the user. Common sense tells me that it should work the same way, but I have learned to abandon common sense when working with Objective-C.

Is there a UIPickerView expert that can help me out here?

Thanks


Solution

  • Whenever you change the contents of the tagPickerData array, you need to call [tagPickerView reloadAllComponents]. The picker doesn’t have any knowledge of its underlying data—you have to send it that message to let it know that it should call its data-source methods.