Reputation: 99
Ok, guys here is my problem, I'm doing a Resistor calculator for electronic students with a uipickerview, but i dont have any idea how to configurate the method of "DidSelectRow".
The idea of this resistor calculator is when the user select the colors (rows) of her resistor, the uipickerview make the operations corresponding to the colors and throw the result in a UILABEL.
For calculate the resistor value, together the value of the line of color 1 to value of the line of color 2 then multiply with the value of the line of color 3, something like that.
This is the image of the values to calculate the resistor value
I dont know how to assing values to the rows, to join them after and then multiply and finally throw in a UILABEL.
Someone help me please!! and i´ll put in the credits of my app! Pleasee :D
This is my .h
#import <UIKit/UIKit.h>
@interface Resistenciacalc : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *ColorPicker;
@property (strong, nonatomic) NSArray*ColorData;
This is my .m
#import "Resistenciacalc.h"
@interface Resistenciacalc ()
@end
@implementation Resistenciacalc
@synthesize ColorData = _ColorData;
@synthesize ColorPicker;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_ColorData = [[NSArray alloc] initWithObjects:@"Negro",@"Marron",@"Rojo",@"Naranja",@"Amarillo",@"Verde",@"Azul",@"Violeta", nil];
}
#pragma mark - UIPickerview Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 4;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return _ColorData.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [_ColorData objectAtIndex:row];
}
/*- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}
Upvotes: 0
Views: 2273
Reputation: 104082
If you put this line: NSLog(@"Row is:%d Component is:%d",row,component); in the pickerView:didSelectRow:inComponent method, you will see that it gives you the row and component (column). The row number will give you the value for that color, since, for instance, "Negro" is in row 0 and black represents 0 in the resistor color scheme. The column number will tell you what value to use as the multiplier to get your resistor value. Something like this should be close to what you need. I've declared properties, firstDigit and secondDigit (as ints) and multiplier (as double) in the controller .h file. I added a button that does the calculation after the user has picked the colors:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (component) {
case 0:
self.firstDigit = row ;
break;
case 1:
self.secondDigit = row ;
break;
case 2:
self.multiplier = pow(10,row);
break;
default:
break;
}
}
-(IBAction)calculateValue:(id)sender {
int value = (self.firstDigit *10 + self.secondDigit)* multiplier;
NSLog(@"%d Ohms",value);
}
Upvotes: 1