gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:03:11 27 4
gpt4 key购买 nike

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

目前这就是我的代码的样子。//.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: 十六进制表示方法,现在实现了它的预期目的:)。

最佳答案

你的委托(delegate)方法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)];

关于iphone - 如何用十六进制数字初始化 uipicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com