2011-06-16 52 views
0

我想使用我的uipicker初始化一個十六進制數的5個字符串表示形式。換句話說,我需要uipicker的每個組件都顯示0-9,A-F的劑量是否有人知道如何做到這一點或者甚至有可能?如何使用十六進制數字初始化uipicker

目前這是我的代碼的外觀。 //.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:爲十六進制表示方法現在這個劑量它是什麼打算這樣做:)。pickerView:titleForRow:forComponent:應該像

回答

1

你的委託方法,

- (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)]; 
+0

那whicked,以便讓我我的字符串,我我也是如何設置每個列的十六進制字符串,目前我只有0-16十進制表示法..但是謝謝你這部分,我需要弄清楚。 – tinhead 2011-06-16 01:51:14

+0

啊..我看到%x會給我十六進制表示!這很酷!雖然,我需要在我的選擇器列中顯示AF .. – tinhead 2011-06-16 01:52:56

+0

使用'return [[NSString stringWithFormat:@「%x」,row] uppercaseString];'而不是'return [NSString stringWithFormat:@「%x」,row] ;'在委託方法中。 – 2011-06-16 01:54:23

相關問題