This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 9th, 2012  |  syntax: Objective C  |  size: 2.91 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #pragma mark -
  2. #pragma mark PickerView Data Source
  3.  
  4. - (NSInteger)numberOfComponentsInPickerView:
  5. (UIPickerView *)pickerView
  6. {
  7.     return 2;
  8. }
  9.  
  10. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  11. {
  12.     if (component == 0)  {
  13.         return [_convertFrom count];
  14.     }
  15.     return [_convertTo count];
  16. }
  17.  
  18.  
  19. - (NSString *) pickerView: (UIPickerView *)pickerView
  20.               titleForRow:(NSInteger)row
  21.              forComponent:(NSInteger)component
  22. {
  23.     if (component == 0) {
  24.         return [_convertFrom objectAtIndex:row];
  25.     }
  26.     return [_convertTo objectAtIndex:row];
  27.    
  28. }
  29.  
  30. #pragma mark -
  31. #pragma mark PickerView Delegate
  32.  
  33. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  34.  
  35. {
  36.    
  37.     [self updateConversionLabel];
  38. }
  39.  
  40.  
  41. - (void)updateConversionLabel
  42. {
  43.     double convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] doubleValue];
  44.    
  45.     double convertTo = [[_convertRates objectAtIndex:[picker selectedRowInComponent:1]] doubleValue];
  46.    
  47.     NSNumberFormatter *fmt = [NSNumberFormatter new];
  48.     double input = [fmt numberFromString:inputText.text].doubleValue;
  49.    
  50.     double to = convertTo;
  51.     double from = convertFrom;
  52.    
  53.     double convertValue = input;
  54.     double relative = to / from;
  55.     double result = relative * convertValue;
  56.    
  57.     NSString *convertFromName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:0]];
  58.     NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]];
  59.    
  60.    
  61.     NSNumber *inputNumber = [[NSNumber alloc ]initWithDouble:convertValue];
  62.     NSNumber *resultNumber = [[NSNumber alloc]initWithDouble:result];
  63.    
  64.    
  65.     NSNumberFormatter *formatterResult = [[NSNumberFormatter alloc] init];
  66.     formatterResult.numberStyle = NSNumberFormatterDecimalStyle;
  67.     NSNumberFormatter *formatterInput = [[NSNumberFormatter alloc] init];
  68.     formatterInput.numberStyle = NSNumberFormatterDecimalStyle;
  69.    
  70.     [formatterResult setNumberStyle:NSNumberFormatterDecimalStyle];
  71.     [formatterResult setMaximumFractionDigits:6];
  72.    
  73.     [formatterInput setNumberStyle:NSNumberFormatterDecimalStyle];
  74.     [formatterInput setMaximumFractionDigits:6];
  75.    
  76.    
  77.    
  78.     NSString *formattedResultString = [formatterResult stringFromNumber:(NSNumber*)resultNumber];
  79.     NSString *formattedInputString = [formatterInput stringFromNumber:(NSNumber*)inputNumber];
  80.    
  81.  
  82.    
  83.    
  84.    
  85.    
  86.     NSString *formelString = [[NSString alloc]initWithFormat:
  87.                               @" %@ %@ =", formattedInputString, convertFromName];
  88.    
  89.     formelLabel.text = formelString;
  90.  
  91.    
  92.     NSString *resultString = [[NSString alloc]initWithFormat:
  93.                               @" %@ %@",formattedResultString, convertToName];
  94.    
  95.     resultLabel.text = resultString;
  96.    
  97.    
  98. }
clone this paste RAW Paste Data