如何使用十六进制数字初始化uipicker

时间:2011-06-16 01:29:12

标签: iphone ios uipickerview uipicker

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

目前这是我的代码的样子。 //的.h

//...

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

    IBOutlet UIPickerView *hexPicker;

//...

//。米

//.....
- (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:十六进制表示的方法,现在可以按照以下方式进行:)

1 个答案:

答案 0 :(得分: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)];