- 0
ios iOS measurement conversion app with picker
-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
Google changes Safe Browsing for Chrome to include real time protection for hostile websites
By John Callaham,
- 12 replies
- 3 views
-
- 6 replies
- 3 views
-
Work on the iOS Epic Games Store can continue as Apple restores Epic's developer account
By John Callaham,
- 9 replies
- 2 views
-
Rift between Apple and Epic Games attracts investigation by the European Commission
By Anushe Fawaz,
- apple
- digital markets act
- (and 10 more)
- 12 replies
- 1 view
-
Apple has canceled Epic Games' developer account, blocking its efforts for its iOS app store
By John Callaham,
- 18 replies
- 1 view
-
Question
WesFromIS
I am building a simple measurement conversion tool (cups to ounces, tsp to tbsp) with a picker (I'd like to use a drop down, but no luck there).
My app launches. The picker populates with options from the datasource.
How ever when I select an option, the app crashes with the following console output
Here are my classes
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) NSArray *conversionType;
@property (strong, nonatomic) NSArray *conversionFactor;
@property (strong, nonatomic) IBOutlet UIView *picker;
@property (strong, nonatomic) IBOutlet UITextField *originalText;
@property (strong, nonatomic) IBOutlet UILabel *resultText;
- (IBAction)textFieldReturn:(id)sender;
@end
[/CODE]
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize picker, originalText, resultText;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_conversionType = @[@"Ounces to Cups", @"Cups to Ounces",
@"Teaspoons to Tablespoons", @"Tablespoons to Teaspoons"];
_conversionFactor = @[ @0.25f, @4.00f, @0.33f, @3.00f];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView Datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _conversionType.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _conversionType[row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
float factor = [_conversionFactor[row] floatValue];
float original = [originalText.text floatValue];
float result = original * factor;
NSString *resultString = [[NSString alloc] initWithFormat:
@"%.2f", result];
resultText.text = resultString;
}
@end
[/CODE]
Thanks for any help you can offer :)
Link to comment
Share on other sites
5 answers to this question
Recommended Posts