2014-02-21 128 views
0

我目前正在研究我的第一个“大”项目。该项目是针对不同货币的simpel转换器。我想知道是否有可能以某种方式反转这两个阵列。我不知道我应该如何执行if语句。如果我把它放在行方法的标题中,我可能会得到控件可能会达到非无效的错误消息。我可以解释这很糟糕,但我刚开始编程。交换两个pickerviews

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]; 

} 

回答

0

我明白你想要做什么。而不是有两个或多个PickerViews。只需更改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]; 
} 
+0

芬兰人拉森:否则创建NS_ENUM识别PickerView数据源应该使用哪一个阵列和数据源方法,比如,pickerView:titleForRow:forComponent:使用的开关情况if-else语句来标识正确的dataSource数组。你能解释更多吗? – user3338161

+0

观看我添加的代码。 –

+0

谢谢,不是我一直在寻找的东西,但我会用它,如果我需要它 – user3338161