Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UIPickerView setup in Xcode?

Hello everyone! I have been working hard lately to understand some of the properties and methods behind some of Xcode's most efficient methods to display friendly user options. Although I have not seen many in today's modern apps, I think the UIPickerView delivers a intuitive way to select a option from several options, with the ability to see them as you scroll.

Here is what I used for the implementation of the main view controller:

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //Set the values for the UIPickerView.
    self.items = @[@"Multiply", @"Divide", @"Subtract", @"Add", @"Square"];

    //Set the data source and delegate for the UIPickerView.
    [self.picker setDataSource:self];
    [self.picker setDelegate:self];
}

- (IBAction)calculateButtonPressed:(id)sender {

    //Set the string value to be the selected calculation type in the UIPickerView.
    NSString *calculationType = [self.items objectAtIndex:[self.picker selectedRowInComponent:0]];

    //We will store the answer in a double.
    float answer = 0;

    //Determine the type of calculation by the selected type, and do that calculation.
    if ([calculationType isEqualToString:@"Multiply"]) {

    //If the type is multiply, simply multiply the text fields text in the form of a floatValue.
        answer = [self.textFieldInputOne.text floatValue] * [self.textFieldInputTwo.text floatValue];
    }

    if ([calculationType isEqualToString:@"Divide"]) {

    //If the type is divide, simply divide the text fields text in the form of a floatValue.
        answer = [self.textFieldInputOne.text floatValue] / [self.textFieldInputTwo.text floatValue];
    }

    if ([calculationType isEqualToString:@"Subtract"]) {

    //If the type is subtract, simply subract the text fields text in the form of a floatValue.    
        answer = [self.textFieldInputOne.text floatValue] - [self.textFieldInputTwo.text floatValue];
    }

    if ([calculationType isEqualToString:@"Add"]) {

    //If the type is add, simply add the text fields text in the form of a floatValue.
        answer = [self.textFieldInputOne.text floatValue] + [self.textFieldInputTwo.text floatValue];
    }

    if ([calculationType isEqualToString:@"Square"]) {

    //If the type is square, simply square the text fields text in the form of a floatValue.
        answer = [self.textFieldInputOne.text floatValue] * [self.textFieldInputOne.text floatValue];
    }

    //Set the answer text to the answer.
    self.answerTextField.text = [NSString stringWithFormat:@"%f", answer];
}


#pragma mark - UIPickerView methods
// The number of columns of data
- (long)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (long)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.items count];
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.items objectAtIndex:row];
}

@end

In the scene view, I have two text fields that are used by the user to input numbers to be calculated in some way. I then use the title of the row at the index of 0 to get the currently selected calculation type to perform that calculation. Under the two text fields for input I use a button to calculate the values within the fields. I have the calculateButtonPressed method assigned as an IBAction. Please note that the Square calculation just multiplies the first text fields float value by the first text fields float value. Finally, I assign the value to the answerTextField, a blank and noneditable text field that will show the answer after a calculation has been performed. Please let me know if you see anything wrong with the code that I have wrote or any changes that can improve performance, I always try to perform calculations in house rather than waste memory for them to be temporarily stored for simple things like in this case a calculation.