0

我想用我的 uipicker 来初始化一个 5 字符的十六进制数字的字符串表示。所以换句话说,我需要uipicker的每个组件显示0-9,AF剂量任何人都知道如何做到这一点,或者甚至可能吗?

目前这就是我的代码的外观。//。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;
}
//...

另外我如何能够在屏幕底部加载选择器。谢谢。 更新了代码来表示这部分 更新了 pickerView:titleForRow:forComponent: 十六进制表示的方法,这现在完成了它打算做的事情:)。

4

1 回答 1

1

你的委托方法pickerView:titleForRow:forComponent:应该是这样的,

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

当您想提取字符串时,请执行此操作,

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

那应该给你你需要的十六进制字符串。

附录

要支持无限滚动,您只需要为这样的行数返回足够大的数字,

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

并在初始化期间使用该selectRow:inComponent:animated:方法在中间某处选择一行,以便用户可以滚动任一方式。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

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

您还需要更改上述方法,

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

十六进制字符串提取将是,

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)];
于 2011-06-16T01:46:24.393 回答