交换两个pickerview



我目前正在进行我的第一个"大"项目。该项目是一个适用于不同货币的简单转换器。我想知道是否有可能以某种方式反转这两个数组。我不知道该如何实现if语句。因为如果我把它放在行方法的标题中,我可能会得到控件可能到达非无效的错误消息。我可能解释得很糟糕,但我刚开始编程。

if(!reverse) { // do nothing } else {
// swap the two pickerviews 
}

//Toggle reverse on/off <->
- (IBAction)Reverse:(id)sender {
reverse = (reverse+1)%2;
NSLog(@"%d",reverse);
}
//what title for row
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [Datarray objectAtIndex:row];
}
return [Datarray2 objectAtIndex:row];
}

我明白你想做什么了。而不是有两个或多个PickerView。只需更改DataSource并重新加载PickerView。如果只有两种货币,只需使用一个标志,否则创建一个NS_ENUM来标识PickerView DataSource应该使用哪个数组,在DataSource方法中,pickerView:titleForRow:forComponent:使用if-else statements的开关大小写来标识正确的DataSource数组。

// .h file
typedef NS_ENUM (NSInteger, CURRENCY_TYPE) {
CURRENCY_TYPE_US,
CURRENCY_TYPE_DK
};
@property (nonatomic, strong) NSArray *dataArrayUS; // In this example, these arrays contains NSStrings
@property (nonatomic, strong) NSArray *dataArrayDK;
@property (nonatomic)         CURRENCY_TYPE currencyType;
// .m file
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
switch (self.currencyType) {
case CURRENCY_TYPE_US:
return self.dataArrayUS[row];
break;
case CURRENCY_TYPE_DK:
return self.dataArrayDK[row];
break;
}
}
- (IBAction)currencyChangingAction:(id)sender
{
// Add some logic that changes to the self.currencyType to the correct currency.
self.currencyType = CURRENCY_TYPE_US; // or _DK
// And Reload Picker
}

编辑:

这是你想要的吗?-不过,只有当您有两个数组时,这才会起作用。

//行的标题是什么

- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return (component == 0) ? [Datarray objectAtIndex:row] : [Datarray2 objectAtIndex:row];
}

最新更新