Using a for loop for a UIPickerView - ios

I am new here and I am getting my hands dirty on some iPhone applications on XCODE. I am currently creating an app but I am stuck on a particular part that involves a pickerview. Here's the code.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if ([pickerView tag] == 0){
return [firstColumn count];
}
else if ([pickerView tag] == 1){
return [OnFirstMin count];
return [OnSecMin count];
return [OnSecSec count];
return [OnFirstSec count];
}
else {
return [OffFirstMin count];
return [OffSecMin count];
return [OffFirstSec count];
return [OffSecSec count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if([pickerView tag] == 0){
if(component == FIRST){
return [firstColumn objectAtIndex:row];
}
}
else if ([pickerView tag] == 1){
if(component == ON1){
return [OnSecMin objectAtIndex:row];
}
else if (component == ON2){
return [OnFirstMin objectAtIndex:row];
}
else if (component == ON3){
return [OnSecSec objectAtIndex:row];
}
else {
return [OnFirstSec objectAtIndex:row];
}
}
else {
if(component == OFF1){
return [OffSecMin objectAtIndex:row];
}
else if (component == OFF2){
return [OffFirstSec objectAtIndex:row];
}
else if (component == OFF3){
return [OffSecSec objectAtIndex:row];
}
else {
return [OffFirstSec objectAtIndex:row];
}
}
return 0;
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if ([pickerView tag] == 0){
if(sets == TRUE && reps != TRUE){
numofsets = row;
SetsTextField.text = [firstColumn objectAtIndex:row];
}
else{
numofreps = row;
RepsTextField.text = [firstColumn objectAtIndex:row];
}
}
else if ([pickerView tag] == 1){
if(component == ON1){
onsecmin = row;
}
else if (component == ON2){
onfirstmin = row;
}
else if (component == ON3){
onsecsec = row;
}
else {
onfirstsec = row;
}
}
else {
if (component == OFF1){
offsecmin = row;
}
else if (component == OFF2){
offfirstmin = row;
}
else if (component == OFF3){
offsecsec = row;
}
else {
offfirstsec = row;
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
firstColumn = [[NSMutableArray alloc] init];
for(i=0; i<=30; i++){
[firstColumn addObject: [NSString stringWithFormat:#"%d", i]];
}
OnSecMin = [[NSMutableArray alloc] init];
OnFirstMin = [[NSMutableArray alloc] init];
for(j=0; j<60; j++){
[OnSecMin addObject:[NSString stringWithFormat:#"%i", j]];
[OnFirstMin addObject: [NSString stringWithFormat:#"%i", j]];
}
OnSecSec = [[NSMutableArray alloc] init]; //Part where I need help.
k =0;
while (k<60){
[OnSecSec addObject: [NSString stringWithFormat:#"%i",k]];
k = k+15;
}
PickerViewSetsRepsContainer.frame = CGRectMake(0, 600, 320, 206);
PickerViewOnContainer.frame = CGRectMake(0, 610, 320, 206);
PickerViewOffContainer.frame = CGRectMake(0, 620, 320, 206);
}
So, for OnSecSec, I want the rows to display 0, 15, 30, and 45 for that component. However, when I run it, the application is aborted. However, when I change that part to a for loop like I did for OnSecMin and OnFirstMin, it works. How come I can't use a while loop like above? Also, can someone help me with this dilemma? Thank you very much for your assistance in advance!

try this
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 1050;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d", component];
NSArray *array = [self valueForKey:arrayName];
[arrayName release];
UIImage *image = [array objectAtIndex:row%7];
if (!view){
view = [[[UIImageView alloc] init] autorelease];
}
[(UIImageView*)view setImage:image];
return view;
}

Related

Objective C how to remove items from array based on other arrays items

I have three UIPickerviews. I have a json response. I have a three text fields. If user select first picker view the related data is displaying in first textfields. And second and third picker view also same. My requirement Is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same.
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
You have most of the code you need already. The pickerView:didSelectRow:inComponent: delegate method will be called when any of your pickers have been changed, assuming they each have their delegates set to the controller above. Although, I only see one picker view defined in the code above. Let's assume your picker views are called myPickerView, myPickerView2, and myPickerView3. The code would roughly be:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (pickerView == pickerView1) {
// the first picker view lseection has changed
// update myPickerView2 and myPickerView3 to reflect
// the values you want based on this selection
} else if (pickerView == pickerView2) {
// update the other views based on this selection
} else ...
....
}

pickerViews crashes

Here is my code:
#synthesize pickerLetter, pickerNumber, pickerSymbol;
- (void)viewDidLoad {
[super viewDidLoad];
letters = [[NSArray alloc]initWithObjects:#"a", #"b", #"c", nil];
numbers = [[NSArray alloc]initWithObjects:#"1", #"2", #"3", nil];
symbols = [[NSArray alloc]initWithObjects:#"+", #"-", #"/", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return letters.count;
} else if (pickerView == pickerNumber){
return numbers.count;
} else {
return symbols.count;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return [letters objectAtIndex:row];
} else if (pickerView == pickerNumber){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}
This is giving me
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
when I use the pickers in the Simulator.
The code that is causing the crash is
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:#"%# %# %#",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}
You've narrowed down your issue to this code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:#"%# %# %#",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}
But this line has several calls to objectAtIndex: so it's hard to know the exact issues. Plus this code is hard to read and impossible to debug. Start by splitting up this code as follows:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:1];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:2];
NSString *symbol = symbols[symbolIndex];
label.text = [NSString stringWithFormat:#"%# %# %#", letter, number, symbol];
}
Doing this you will be able to narrow down the real cause of your issue.
As you can see, the problem is that you are referencing the wrong component number from the pickerNumber and pickerSymbol. All three pickers only have 1 component so you need to select component 0 from all three pickers.
The needed code is:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:0];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:0];
NSString *symbol = symbols[symbolIndex];
label.text = [NSString stringWithFormat:#"%# %# %#", letter, number, symbol];
}
Avoid putting more than one or two method calls on a single line of code. It makes the code less readable and much harder to debug if there is a problem.
you are calling a wrong way to implement the method
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return [letters objectAtIndex:row];
} else if (pickerView == pickerNumber){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}
change it to
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [letters objectAtIndex:row];
} else if (component == 1){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}

button tag issue with UISearchBar

I need to set a tag for the button created on the table view cell.
I implemented the UISearchBar and it works well.
Issue is after filtering the tableView cell, its indexpath.row changes as I am setting
'button.tag = indexPath.row'.
Is there a way to keep the row number constant for every cell?
Or is there any other solution?
Note: I have multiple sections in one UITableView and isFiltered is BOOL value which indicates user have started to type text in UISearchBar.
Implemented Searching with help of https://github.com/kwylez/IndexedTable
In cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"CheckBoxedCell";
// NSString *cellId = [NSString stringWithFormat:#"Section:%d Row:%d",indexPath.section,indexPath.row];
CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
NSArray *nib;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass" owner:self options:nil];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass_iPad" owner:self options:nil];
}
for (id object in nib)
{
if([object isKindOfClass:[CheckBoxedCellClass class]])
{
cell = (CheckBoxedCellClass *)object;
break;
}
}
cell = [nib objectAtIndex:0];
}
SaveCheckBoxedView *saveContact;
if(isFiltered == YES)
{
saveContact = [filterdArray objectAtIndex:indexPath.row];
cell.nameLabel.text = saveContact.nameString;
}
else
{
saveContact = [mutableArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}
//cell.nameLabel.text = saveContact.nameString;
cell.companyLabel.text = saveContact.companyString;
cell.invIdLabel.text = [NSString stringWithFormat:#"%d", saveContact.invitId];
//set fonts
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]];
}
else
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]];
}
//handling check box
NSInteger rowNumber = 0;
for(NSInteger i = 0; i < indexPath.section ; i++)
{
rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
/*if([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *finalIntId = [mutableArrayOfIds objectAtIndex:rowNumber];
NSLog(#"Tagged checked button id = %#", finalIntId);
[arrayOfIds addObject:finalIntId];
}*/
UIButton *checkBox;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
}
else
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
}
[checkBox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:#selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];
if(isFiltered == YES)
{
checkBox.tag = ;
}
else
{
checkBox.tag = rowNumber;
}
[cell.contentView addSubview:checkBox];
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(isFiltered == YES) {
return Nil;
} else {
NSArray *toBeReturned = [NSArray arrayWithArray:
[#"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:#"|"]];
return toBeReturned;
}
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if (title == UITableViewIndexSearch) {
CGRect searchBarFrame = self.searchDisplayController.searchBar.frame;
[tableView scrollRectToVisible:searchBarFrame animated:YES];
return -1;
} else {
NSInteger count = 0;
for (NSString *character in arrayOfCharacters) {
if ([character isEqualToString:title]) {
return count;
}
count ++;
}
return 0;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(isFiltered == YES) {
return 1;
} else {
return [arrayOfCharacters count];
//return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isFiltered == YES) {
return [filterdArray count];
} else {
//return [mutableArray count];
return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([arrayOfCharacters count] == 0) {
return #"";
}
return [NSString stringWithFormat:#"%#", [arrayOfCharacters objectAtIndex:section]];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFiltered = NO;
}
else
{
isFiltered = YES;
filterdArray = [[NSMutableArray alloc] init];
for (SaveCheckBoxedView *contact in mutableArray)
{
NSRange nameRange = [contact.nameString rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound)
{
[filterdArray addObject:contact];
}
}
}
[self.tableViewContact reloadData];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filterdArray removeAllObjects];
if(searchString.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.mySearchBar.text];
for (NSString *key in arrayOfCharacters)
{
NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
[filterdArray addObjectsFromArray:matches];
}
}
return YES;
}
I am describing one way to do so, you may find better way.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filterdArray removeAllObjects];
if(searchString.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.mySearchBar.text];
for (NSString *key in arrayOfCharacters)
{
NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
/****see bellow****/
[filterdArray addObjectsFromArray:matches];
}
}
return YES;
}
Try to find the row number of each object of "matches". that will be the tag of your buttons. Make filterdArray an array of dictionary. Add 2 field to dictionary. one for tag another for value. in cellForRowAtIndexPath.
if(isFiltered == YES)
{
checkBox.tag = [filterdArray objectForKey: #"tag"] ;
}
else
{
checkBox.tag = rowNumber;
}

Insert element at NSMutableArray's end

I am loading an array from a web service and displaying them in picker and all works fine. My array contains random number of questions for every category of questions. Now what i want is that i wanna add one more entry "Your own question" at the end of array so that the last row of picker displays "Your own question". How can i do this
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
retval.text = [quesArray objectAtIndex:row];
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}
This code only displays number of rows(questions) fetched from the web service.
EDIT 1:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count];
}
}
EDIT 2:
Resolved by making another NSMutableArray with my custom object "Your own question" and then appending it with the array fetched from web service
NSMutableArray *customObj = [NSMutableArray arrayWithObject: #"test"];
[quesArray addObjectsFromArray:customObj];
In UIPickerViewDataSource's method called pickerView:numberOfRowsInComponent: return your array's count plus one value(return arr.count+1;), and in the method of UIPickerVIewDelegatethat you have implemented return your label with the text when the component is greater than arr.count-1 (if(row>(arr-1)){//return the label with 'Your own question'}). Good Luck!
EDIT:
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count]+1;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
if(row>quesArray.count-1){
retval.text = #"You own question";
}
else{
retval.text = [quesArray objectAtIndex:row];
}
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}

how to call a conversion using double component UIPickerview

I will post my code below as a quick note I define both of the components in my .h file and I need to call an action when certain rows are picked. For example something like if row in component == 0 and other component == 1 do this?
Still new to iPhone Programing thank you in advanced!!!
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component{
if (component==kapickerComponent) {
return [parentarray count];
}
if (component == kbpickerComponent) {
return [subarray count];
}
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)rowforComponent:(NSInteger)component{
if (component == kapickerComponent) {
return [self.parentarray objectAtIndex:row];
}
if (component == kbpickerComponent) {
return [self.subarray objectAtIndex:row];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ( row. kapickerComponent ==0, and row.kbpickerComponent==1 ??????????? ){
float number1 = ([initamount.text floatValue ]);
initamount.text = [[NSString alloc]initWithFormat:#" ", number1];
double answer = number1 *12;
result.text = [[NSString alloc]initWithFormat:#" ", answer];
}
}
Here:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ([thePickerView selectedRowInComponent:kapickerComponent] == 0 && [thePickerView selectedRowInComponent:kbpickerComponent] == 1) {
float number1 = [initamount.text floatValue];
initamount.text = [[NSString alloc]initWithFormat:#"%f", number1];
double answer = number1 *12;
result.text = [[NSString alloc] initWithFormat:#"%f", answer];
}
}
if ( kapickerComponent.row == 0 && kbpickerComponent.row
==1){// Ur CODE HERE}

Resources