How to Implement a UIPickerView in Objective-C for a Converter App with Multiple Segments

Objective-C Converter App: A Deep Dive into UIPickerView and Conversion Formulas

As a developer working on a small converter app for a school project, you’re likely looking for ways to make your code more efficient and effective. In this article, we’ll take a closer look at how to implement a UIPickerView in Objective-C and create conversion formulas for different units of measurement.

Introduction

A UIPickerView is a UI component that allows users to select values from a list of options. In the context of a converter app, Pickers can be used to display different units of measurement. For example, you might have a Picker for distance measurements, where the user can select between meters, inches, and yards.

Initializing the Picker Sections

To initialize the Picker sections, we need to create an array of arrays that represents the data structure for our Picker. Each inner array contains two values: the first value is the label for the Picker segment, and the second value is an array of conversion factors.

Here’s an example of how you might implement this:

@interface konverterViewController : UIViewController

@property (nonatomic, strong) NSArray *fizVelicine; // First Picker segment
@property (nonatomic, strong) NSArray *udaljenostJedinice; // Second Picker segment
@property (nonatomic, strong) NSArray *masaJedinice; // Third Picker segment
@property (nonatomic, strong) NSArray *conversions; // Conversion factors array

@end

@implementation konverterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fizVelicine = @[@"distance", @"mass"];
    self.udaljenostJedinice = @[@"meter", @"kilometer", @"yard", @"inch"];
    self.masaJedinice = @[@"kilogram",@"dekagram",@"gram",@"tone"];
    self.conversions = @[
        @"\@[@"
        @"\t meters", // 1.0 * meters
        @"\t inches", // 39.37 * inches
        @"\t cubits", // 2.19 * cubits
        @"\t whatevers", // conversion factor for unknown unit
        @"\@"
    ];
}

In this example, we create an array of arrays that represents the data structure for our Picker. The first element in each inner array is the label for the Picker segment, and the second element is an array of conversion factors.

Computing Conversion Formulas

To compute the conversion formula, we need to get the current Picker selections and look up a conversion factor from our conversions array. We can do this by accessing the selected row in each Picker component and using that value to index into our conversions array.

Here’s an example of how you might implement this:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row   inComponent:(NSInteger)component {
    if (component == 0) {
        self.odabir = [NSString stringWithFormat:@"%@" , [self.fizVelicine objectAtIndex:row]];
        
        [pickerView reloadAllComponents];
    }
    
    // Get the current Picker selections
    NSInteger fromUnitsIndex = [self.picker selectedRowInComponent:1];
    NSArray *fromArray = self.conversions[fromUnitsIndex];

    // The second member of this array is an array of conversion factors
    NSArray *conversionNumbers = fromArray[1];

    NSInteger toUnitsIndex = [self.picker selectedRowInComponent:2];
    NSNumber *conversionNumber = conversionNumbers[toUnitsIndex];
}

In this example, we get the current Picker selections and use that information to compute the conversion formula. We access the selected row in each Picker component and use that value to index into our conversions array.

Conclusion

Implementing a UIPickerView in Objective-C and creating conversion formulas for different units of measurement can be a complex task, but it’s also a fun project. By following the steps outlined in this article, you should be able to create a working Picker app with multiple segments that display different units of measurement.

Remember to always keep your code organized and well-structured, and don’t be afraid to ask for help if you’re stuck. Good luck with your project!

Additional Tips

  • When implementing the conversion formulas, make sure to handle cases where the selected unit is not a valid conversion factor.
  • Consider using a data-driven approach to manage your Picker segments and conversion factors, rather than hardcoding them into your code.
  • Use descriptive variable names and comments to help explain what each section of your code is doing.

Conversion Formulas Example

Here’s an example of how you might implement the conversion formulas for different units of measurement:

// Distance conversions
double metersToInches(double meters) {
    return meters * 39.37;
}

double metersToYards(double meters) {
    return meters / 1.09361;
}

double inchesToMeters(double inches) {
    return inches / 39.37;
}

double inchesToYards(double inches) {
    return inches / 36;
}

double yardsToMeters(double yards) {
    return yards * 0.9144;
}

In this example, we define a set of conversion formulas for different units of measurement. We can then use these formulas to compute the conversion value when the user selects a new unit.

Note that these formulas assume that the input and output values are in the same unit system (e.g., both meters or inches). If you need to perform conversions between different unit systems, you’ll need to add additional logic to handle those cases.


Last modified on 2023-05-29