Can't compare values from my UIPickerView - iphone

I'm trying to output a certain number based on the first and second selection in my UIPickerView. I have this code here.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if([[list objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: #"1 Gallon"] && [list2 objectAtIndex:[pickerView selectedRowInComponent:1] isEqual: #"Grams"])
output.text = #"1 GALLON GRAMS";
if([[list objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: #"2 Gallons"] && [list2 objectAtIndex:[pickerView selectedRowInComponent:1] isEqual: #"Ounces"])
output.text = #"2 GALLONS OUNCES";
}
But I keep getting an error of No visible #interface for NSMutableArray declares the selector objectAtIndex:isequal
How should I compare what the two values are returning? Like IF comp(0) == 3 and comp(1) ==2, output 3.4 to my label.

That's because NSMutableArray doesn't have an "objectAtIndex: isEqual" selector.
You need to get the NSString from your picker view and then compare it to the string you've stored in your NSMutableArray object (which are presumably ivars named "list" and "list2").
E.G.
NSString * stringPointedToFromPicker = (NSString *)[list objectAtIndex: [pickerView selectedRowInComponent: 0]];
if([stringPointedToFromPicker isEqual: #"1 Gallon"]) // okay for now, but do you want
output.text = #"1 GALLON GRAMS"; // hard coded, non-localized strings like this?

Related

How to fill pickerView properly

i have an UIPickerView and i want it to have 2 columns filled with items from my arrays, but i cant figure out how, there is my arrays:
-(void)fillingStandartWeightArray{
if (!standartWeightArray){
for (int i=25; i<150 ;i++){
NSString *weightString;
weightString = [NSString stringWithFormat:#"%d kg", i];
[standartWeightArray addObject:weightString];
NSLog(#"%#", weightString);
}
}
}
-(void)fillingStandartHeightArray{
if (!standartHeightArray){
for (int i=85; i<250; i++){
NSString *heightString;
heightString = [NSString stringWithFormat:#"%d cm", i];
[standartHeightArray addObject:heightString];
NSLog(#"%#", heightString);
}
}
}
To be more specific i don't know how to "tell" UIPickerView to fill one of it wheel with one array, and other one with other array. I tried this:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0){
return [standartWeightArray count];
} else {
return [standartHeightArray count];
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0)
return [standartWeightArray objectAtIndex:row];
else if (component == 1)
return [standartHeightArray objectAtIndex:row];
}
And i did call functions in viewDidLoad sections:
- (void)viewDidLoad
{
[super viewDidLoad];
[self fillingStandartWeightArray];
[self fillingStandartHeightArray];
NSLog(#"%#", standartWeightArray);
}
But that's not working.
Your datasource/delegate methods are perfect.
Take care of following things
1) Allocating standartHeightArray and standartWeightArray objects properly. Allocate array objects as below if you are not doing already.
-(void)fillingStandartWeightArray{
if (!standartWeightArray){
// Allocate the array
standartWeightArray = [NSMutableArray new];
for (int i=25; i<150 ;i++){
NSString *weightString;
weightString = [NSString stringWithFormat:#"%d kg", i];
[standartWeightArray addObject:weightString];
NSLog(#"%#", weightString);
}
}
}
-(void)fillingStandartHeightArray{
if (!standartHeightArray){
// Allocate the array
standartHeightArray = [NSMutableArray new];
for (int i=85; i<250; i++){
NSString *heightString;
heightString = [NSString stringWithFormat:#"%d cm", i];
[standartHeightArray addObject:heightString];
NSLog(#"%#", heightString);
}
}
}
2) Connect the delegate/datasource properties of picker properly. You can do it in interface builder or in code (in viewDidLoad)
Sure looks like you are setting everything up correctly to me.
But make sure you set the UIPickerView delegate and data source to the same object / class (ViewController?) where your PickerView data source methods live.
You can set this either in Xcode's Interface Builder, or at "viewDidLoad:" time programmatically (if you have your picker view set to an IBOutlet).

My label can't get value from pickerview

My numberOfComponentsInPickerView :
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
My numberOfRowsInComponent :
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return 100;
if (component == 1)
return 100;
if (component == 2)
return 100;
return 0;
}
My titleForRow like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [NSString stringWithFormat:#"%d", row];
if (component == 1)
return [NSString stringWithFormat:#"%d", row];
if (component == 2)
return [NSString stringWithFormat:#"%d", row];
return 0;
}
my didSelectRow like this
after edited like Paras Joshi's said :
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
int year = [picker selectedRowInComponent:0];
int month = [picker selectedRowInComponent:1];
int day = [picker selectedRowInComponent:2];
if(viewPicker.tag == 1)
labelDate1.text = [year stringByAppendingFormat:#" : %d : %d", month, day];
else
labelDate2.text = [year stringByAppendingFormat:#" : %d : %d", month, day];
}
it still gives me error " bad receiver type 'int' " and i still don't get it how to fix it. how my label get data from titleForRow?
both input for year month and day all are number only (from 0 to 99) so that i wrote -> return [NSString stringWithFormat:#"%d", row];
i dont put data for my pickerview at - (void)viewDidLoad because i want my labelDate1 or labelDate2 got data from pickerview. is there any possible my label get data from pickerview like i wrote above? or must i write my data at - (void)viewDidLoad ?
for any help, thank you for watching my question.
In your program you are appending string to an integer that is the issue.
This code makes the issue. Because year is an integer.
labelDate1.text = [year stringByAppendingFormat:#" : %d : %d", month, day];
Please check with this code.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
int year = [picker selectedRowInComponent:0];
int month = [picker selectedRowInComponent:1];
int day = [picker selectedRowInComponent:2];
NSString *date = #"";
if(viewPicker.tag == 1)
labelDate1.text = [date stringByAppendingFormat:#" %d : %d : %d",year, month, day];
else
labelDate2.text = [date stringByAppendingFormat:#"%d : %d : %d", year, month, day];
}
Did you set the UIPickerView's delegate?I mean, did your pickerView:didSelectRow:inComponent get called?
Hello #Piyo Piyo your
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
method is totally wrong, you are assigning NSInteger to NSString. Thats why you are getting en error. You should use NSArray for putting data in picker view row and picking data from here. Here i'm going to make a small sample code.
This is your viewController.h file...
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *myPickerView;
NSMutableArray *arrayYear;
NSMutableArray *arrayMonth;
NSMutableArray *arrayDay;
UILabel *lblDay;
UILabel *lblMonth;
UILabel *lblYear;
}
#end
there are three array which will contain the data for day, month and year respectively and three UILabel for showing the data. and obviously one pickerview.
Now come to on your viewController.m part.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UIPickerView declaration by coding
myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 216.0)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
// add some day number to day array
arrayDay = [[NSMutableArray alloc]init];
for(int i=1;i<6;i++)
{
NSString *string = [NSString stringWithFormat:#"%d",i];
[arrayDay addObject:string];
}
// add some month string to month array
arrayMonth = [[NSMutableArray alloc]init];
[arrayMonth addObject:#"June"];
[arrayMonth addObject:#"July"];
[arrayMonth addObject:#"August"];
[arrayMonth addObject:#"September"];
[arrayMonth addObject:#"October"];
// add some year number to year array
arrayYear = [[NSMutableArray alloc]init];
for(int i=2006;i<2011;i++)
{
NSString *string = [NSString stringWithFormat:#"%d",i];
[arrayYear addObject:string];
}
//set initially text to day label
lblDay = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 250.0, 90.0, 50.0)];
[lblDay setText:[arrayDay objectAtIndex:0]];
[self.view addSubview:lblDay];
//set initially text to month label
lblMonth = [[UILabel alloc]initWithFrame:CGRectMake(110.0, 250.0, 90.0, 50.0)];
[lblMonth setText:[arrayMonth objectAtIndex:0]];
[self.view addSubview:lblMonth];
//set initially text to year label
lblYear = [[UILabel alloc]initWithFrame:CGRectMake(210.0, 250.0, 90.0, 50.0)];
[lblYear setText:[arrayYear objectAtIndex:0]];
[self.view addSubview:lblYear];
//set initially selection to each component of UIPickerView
[myPickerView selectRow:1 inComponent:0 animated:NO];
[myPickerView selectRow:1 inComponent:1 animated:NO];
[myPickerView selectRow:1 inComponent:2 animated:NO];
}
//here you are returning the number of component, which are 3 in this case
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 3;
}
//this is your didSelectRow method and here you are assigning a new text to
//label accordingly to if condition
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
lblDay.text = [arrayDay objectAtIndex:row];
}
if(component == 1)
{
lblMonth.text = [arrayMonth objectAtIndex:row];
}
if(component == 2)
{
lblYear.text = [arrayYear objectAtIndex:row];
}
}
//this is your numberOf row in component in this case each component have 5
//rows thats why i wrote only return 5; here you can put if condition here for
//returning different rows for each component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return 5;
}
// and this is showing title on rows
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (component == 0)
return [arrayDay objectAtIndex:row];
else if (component == 1)
return [arrayMonth objectAtIndex:row];
else if (component == 2)
return [arrayYear objectAtIndex:row];
else
return 0;
}
I hope this tutorial will help you. Thank you!

Make UIPicker have at least one foot value?

Just like with Apples timer app, you can't select "0". You have to choose at least 1 hour or 1 minute. Here is how I'm formatting the textfield for the picker..
//Formats the textfield based on the pickers.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *result = [feetArray objectAtIndex:[feetPicker selectedRowInComponent:0]];
result = [result stringByAppendingFormat:#"%#ft", [feetArray objectAtIndex:[feetPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:#" %#", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:0]]];
result = [result stringByAppendingFormat:#"%#", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:#" %#in", [fractionArray objectAtIndex:[fractionPicker selectedRowInComponent:0]]];
RiseTextField.text = result;
}
What I need to if they choose "0" all the way across, I want the foot to automatically go to 1 on the picker. I have 3 pickers in my view made as one. 2 components for feet, 2 components for inches then 1 component for fractions. so its displayed in the textfield like 10f 09 1/16in.
Essentially what you want to do is this:(assuming that row 0 contains the zero values)
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (([feetPicker selectedRowInComponent:0] == 0)&&
([feetPicker selectedRowInComponent:1] == 0)&&
([inchesPicker selectedRowInComponent:0] == 0)&&
([inchesPicker selectedRowInComponent:1] == 0)&&
([fractionPicker selectedRowInComponent:0] == 0)){
// All values zero
[feetPicker selectRow:1
inComponent:1
animated:YES];
return;
}
// Handle valid value
}
On a side note consider using an NSMutableString for result. Then instead of creating a new NSString every line you can just call [result appendFormat:#".... to modify the one you have.

How to set Grouped UITableview Section names without a NSFetchedResultsController in play

I have used a number of grouped tables tied to core data managed objects, where the sectionNameKeyPath value is used to identify the attribute in the data that should be used to denote sections for the table.
But how do I indicate the "sectionNameKeyPath equivalent" when I have a table that is being use to present an NSMutableArray full of objects that look like this:
#interface SimGrade : NSObject {
NSNumber * scoreValue;
NSString * commentInfo;
NSString * catName;
}
I would like to have sections defined according to the "catName" member of the class.
Consider, for example that my mutablearray has 5 entries where the 5 "catName" values are "Blue", "Blue", "Blue", "Red", and "Red". So I'd want the number of sections in the table for that example to be 2.
So, what I would 'like to do' could be represented by the following pseudo-code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return (The number of unique catNames);
}
Note: My interest in doing this is not so much for displaying separate sections in the table, but rather so that I can more easily calculate the sums of scoreValues for each category.
<<<<< UPDATE >>>>>>>>>
Joshua's help, as documented in his response has been right on. Here are the two new handlers for number of sections and number of rows per section...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSMutableSet *counter = [[NSMutableSet alloc] init];
[tableOfSimGrades enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
[counter addObject:[object catName]];
}];
NSInteger cnt = [counter count];
[counter release];
NSLog(#">>>>> number of sections is -> %d", cnt);
return cnt;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSMutableDictionary *counter = [[NSMutableDictionary alloc] init];
NSMutableArray *cats = [[NSMutableArray alloc] init];
__block NSNumber *countOfElements;
[tableOfSimGrades enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// check the dictionary for they key, if it's not there we get nil
countOfElements = [counter objectForKey:[object catName]];
if (countOfElements) {
// NSNumbers can't do math, so we use ints.
int curcount = [countOfElements intValue];
curcount++;
[counter setObject:[NSNumber numberWithInt:curcount] forKey:[object catName]];
NSLog(#">>>> adding object %d to dict for cat: %#", curcount, [object catName]);
} else {
[counter setObject:[NSNumber numberWithInt:1] forKey:[object catName]];
[cats addObject:[object catName]];
NSLog(#">>>>> adding initial object to dict for cat: %#", [object catName]);
}
}];
countOfElements = [counter objectForKey:[cats objectAtIndex: section]];
int catcount = [countOfElements intValue];
[counter release];
[cats release];
return catcount;
}
My current issue with this routine now lies in the following function... It is ignorant of any sections in the nsmutableArray and so for each section, it starts at index 0 of the array instead of at the 0th element of the appropriate section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
SimGrade *tmpGrade = [[SimGrade alloc] init];
tmpGrade = [tableOfSimGrades objectAtIndex: indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Category: %#", tmpGrade.catName];
// [tmpGrade release];
return cell;
}
How do I transform the "indexpath" sent to this routine into the appropriate section of the mutableArray?
Thanks,
Phil
You could do something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSMutableSet *counter = [[NSMutableSet alloc] init];
[arrayOfSims enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
[counter addObject:object.catName];
}];
NSInteger cnt = [counter count];
[counter release];
return cnt;
}
you'd probably want to memoize that, for performance reasons (but only after profiling it).
--- EDIT ---
You can use an NSMutableDictionary, too, to get counts of individual categories.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSMutableDictionary *counter = [[NSMutableDictionary alloc] init];
__block NSNumber *countOfElements;
[arrayOfSims enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// check the dictionary for they key, if it's not there we get nil
countOfElements = [counter objectForKey:object.catName];
if (countOfElements) {
// NSNumbers can't do math, so we use ints.
int curcount = [countOfElements intValue];
curcount++;
[counter setObject:[NSNumber numberWithInt:curcount] forKey:object.catName];
} else {
[counter setObject:[NSNumber numberWithInt:1] forKey:object.catName];
}
}];
NSInteger cnt = [counter count];
// we can also get information about each category name, if we choose
[counter release];
return cnt;
}

Solve Simple NSCSF Number Problem

Getting the error: -[NSCFNumber length]: unrecognized selector sent to instance at line [firstComponentText setString:[pickerArray objectAtIndex:row]];.
-(void) viewDidLoad
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( float i = 0.0 ; i <= 1000.0 ; i = i + 2.5)
[pickerArray addObject:[NSNumber numberWithFloat:i]];
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView.tag==1)//weight
{
[firstComponentText setString:[pickerArray objectAtIndex:row]];
weightLabel.text = firstComponentText;
}
}
Does your pickerArray contain NSNumber objects? If so, to get an NSString, you'll need to use a method like +[NSString stringWithFormat:] or -[NSNumber stringValue].
If that's the case, try either of the following:
[firstComponentText setString:[[pickerArray objectAtIndex:row] stringValue]];
or
[firstComponentText setString:[NSString stringWithFormat:#"%#", [pickerArray objectAtIndex:row]]];