I am working on a picker view with 3 columns. I want to determine the 3rd column value from the second column. Here is part of my code
- (void)viewDidLoad {
[super viewDidLoad];
Number=@[@"Trans",@"1st",@"2nd",@"3rd",@"4th",@"5th",@"6th",@"7th",@"8th",@"9th",@"10th",@"11th",@"12th"];
Season=@[@"Spring",@"Summer",@"Fall"];
Course=@[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
// Course=@[@"Summer1",@"Summer2",@"Summer3"];
Course = [Course sortedArrayUsingSelector:@selector(compare:)];
Number =[Number sortedArrayUsingSelector:@selector(compare:)];
Season =[Season sortedArrayUsingSelector:@selector(compare:)];
}
Here is my question, how can I achieve the goal like
if (Season==@"Spring")
Course= @[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
else if (Season==@"Summer")
Course=@[@"Summer1",@"Summer2",@"Summer3"];
Sorry, I don't know what method to use to complete the code. Any idea please?
To show the column value, I use the following code:
- (IBAction)addCourse:(UIButton *)sender {
NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];
NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];
NSString *msg=[[NSString alloc ]initWithFormat:@"%@ ",course];
_courseLabel.text=[msg substringToIndex:8];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==kNumComponent)
return [Number count];
else if(component==kSeaComponent)
return [Season count];
else return [Course count];
}
According to the documentation, you must provide your UIPickerView with a datasource and a delegate. The datasource tells the view how many components and rows there are, while the delegate tells it the content of the components row. Let us suppose that you implement titleForRow: in your delegate. Since this is going to change based on a selection, you will need to do two things: (i) make sure your delegate object knows which selection was made (e.g. @"Spring") and (ii) you will then need to call the UIPickerView reloadComponent: method so that your delegate's titleForRow: method will be called.
Example: suppose your action method is componentSelected:
-(void)componentSelected:(id)sender
{
NSInteger row = [myPicker selectedRowInComponent:seasonComponent];
myDelegate.season = row;
[myPicker reloadComponent:courseComponent];
}
By the way, it is worth getting into the habit of being very careful with "=", "==", isEqualToString: etc. to avoid bugs. Your question has several basic syntactical errors. The line
if(season = @"spring")
should be
if([season isEqualToString:@"spring"]);
not just because "=" is assignment, but because "==" makes no sense if one of the arguments is a literal (it is essentially pointer comparison). isEqualToString will compare the target string with the argument string.