iphoneiosuipickerviewuipicker

how to initialize uipicker with hex digits


I want to use my uipicker to initialize a 5character string representation of a hex number. So in other words I need each component of the uipicker to show 0-9,A-F dose anyone have an idea on how this could be done or if it is even possible?

currently this is how my code looks. //.h

//...

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {

    IBOutlet UIPickerView *hexPicker;

//...

//.m

//.....
- (void)viewDidLoad {

    //hexPicker = [[UIPickerView alloc] init];
//initalise icker at bottom of screen
hexPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 244, 320, 216)];
    hexPicker.delegate = self;
    hexPicker.dataSource = self;
    hexPicker.showsSelectionIndicator = YES;


    [self.view addSubview:hexPicker];


    [super viewDidLoad];
}
//.....

#pragma mark UIPickerViewDelegate methods

- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//This only shows as digits/integers
    //return [NSString stringWithFormat:@"%d",row];
//this shows those digits as hex
[[NSString stringWithFormat:@"%x",row] uppercaseString];
}

#pragma mark UIPickerViewDataSource methods

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

- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
    return 16;
}
//...

Also how am I able to load the picker at the bottom of the screen. Thanks. Updated code to represent this part Updated pickerView:titleForRow:forComponent: method for hex representation this now dose what it is intended to do :).


Solution

  • Your delegate method pickerView:titleForRow:forComponent: should be like,

    - (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [NSString stringWithFormat:@"%X",row];
    }
    

    When you want to extract the string do this,

    NSString *hexString = [NSString stringWithFormat:@"%X%X%X%X%X", [hexPicker selectedRowInComponent:0], [hexPicker selectedRowInComponent:1], [hexPicker selectedRowInComponent:2], [hexPicker selectedRowInComponent:3], [hexPicker selectedRowInComponent:4]];
    

    That should give you the hex string you need.

    Addendum

    To support infinite scroll, you will just need to return a sufficiently huge number for the number of rows like this,

    - (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
    {
        return 160000;
    }
    

    and during initialization use the selectRow:inComponent:animated: method to select a row somewhere in the middle so that user can scroll either ways.

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        for ( int i = 0; i < hexPicker.numberOfComponents; i++ ) {
            [hexPicker selectRow:80000 inComponent:i animated:NO];
        }
    }
    

    You will also need to change the methods mentioned above,

    - (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [NSString stringWithFormat:@"%X",(row % 16)];
    }
    

    And the hex string extraction would be,

    NSString *hexString = [NSString stringWithFormat:@"%X%X%X%X%X", ([hexPicker selectedRowInComponent:0] % 16), ([hexPicker selectedRowInComponent:1] % 16), ([hexPicker selectedRowInComponent:2] % 16), ([hexPicker selectedRowInComponent:3] % 16), ([hexPicker selectedRowInComponent:4] % 16)];