0

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 :).

Cœur
  • 37,241
  • 25
  • 195
  • 267
tinhead
  • 385
  • 2
  • 10
  • 29

1 Answers1

1

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)];
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Thats whicked, so that gets me my string, I was also wounder how I set each column with a hex string, at the moment I only have 0-16 decimal representation.. but thankyou for this part that I needed to figure out. – tinhead Jun 16 '11 at 01:51
  • Ahh.. I see the %x will give me hex representation! thats pretty cool! although, I need to show A-F in my picker columns.. – tinhead Jun 16 '11 at 01:52
  • use `return [[NSString stringWithFormat:@"%x",row] uppercaseString];` instead of `return [NSString stringWithFormat:@"%x",row];` in the delegate method. – Deepak Danduprolu Jun 16 '11 at 01:54
  • yep that uppercasestring worked like a dream.. Just a final question, do you know how to achieve the infinite scroll? i.e. so it loops back through the hex digits? – tinhead Jun 16 '11 at 02:00
  • Added some more details to the answer. – Deepak Danduprolu Jun 16 '11 at 02:14
  • 1
    For upper case Hex just use %X (uppercase X). – zaph Jun 16 '11 at 02:44
  • @CocoaFu Yeah you're right. Sometimes it just has to be complicated. Edited the answer to reflect that. – Deepak Danduprolu Jun 16 '11 at 02:52