I have 3 UITextField's, date1, date2(UIDatePicker) and station name(Like combobox). First of all I must load all station name to station name UITextField.When I enter A listing starting with A station name.I select first date,second date and station name for send to web service for display fields on labels.I want to sending station ID when I select station name.Every station name have station ID.
Anybody have idea or sample ? Thanks.
if you are using picker view then try this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(row == 0)
{
return #"Station 1";
}
else if(row == 1)
{
return #"Station 2";
}
else if (row==2)
{
return #"Station 3";
}
return #"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(row == 0)
{
self.int_Stationid = 1;
}
else if(row == 1)
{
self.int_Stationid = 2;
}
else if (row==2)
{
self.int_Stationid = 3;
}
}
Related
i tired two component in picker view i.e (currency),(id) and showed but my question is how to hide "id" component alone in picker view."currency" in responseArray and "id" in responseArray1.
picker view delegate:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
return [responseArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{
//return [responseArray objectAtIndex:row];
if (component == 0) {
return [responseArray objectAtIndex:row];
} else {
return [responseArray1 objectAtIndex:row];
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
NSLog([responseArray1 objectAtIndex:row]);
if (component == 0) {
[pickerView selectRow:row inComponent:1 animated:YES ];
} else if(component == 1)
{
if (row != [pickerView selectedRowInComponent:0])
{
[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:YES];
}
}
}
Answer as per comments :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [responseArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [responseArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"rsponse currency : %#",[responseArray objectAtIndex:row]);
NSLog([responseArray1 objectAtIndex:row]);
// You can fetch value from both array using row parameter.
// You can display currency and id for particular row from here like above
}
You not need to display two component in pickerview. Just display one which you want to show and according to selected row you can fetch respective element from array.
Hope this will help :)
I am using a UIpicker with three components. I want if I select a row in first component on the basis of selected component it shows the value of the corresponding data in second component.
i used array for first component value like #"UK",#"US",#"EU".now i want if user selected UK in first component then i want to load UKsize1=[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil]; in second component , if user selected US in first component then i want EUsize2=[NSArray arrayWithObjects:#"11",#"22",#"33",#"44",#"55", nil]; load in second component. same like for EU value then i want to load USsize3=[NSArray arrayWithObjects:#"111",#"222",#"333",#"444",#"555", nil];. i want second component value changed based on the first component , third component value is fixed.
this code i tried but i am not getting exact value.
- (id)init
{
if (self = [super init]) {
notesToDisplayForKey = [NSArray arrayWithObjects: #"UK", #"US", #"EU", nil];
scaleNames = [NSArray arrayWithObjects: #"11", #"22", #"33", #"555",#"666",#"777",nil];
scaleValue=[NSArray arrayWithObjects: #"1" ,#"2", #"3", #"4",#"5",#"6", #"7",#"8",#"9", #"10",#"11",nil];
UKsize1=[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
EUsize2=[NSArray arrayWithObjects:#"11",#"22",#"33",#"44",#"55", nil];
USsize3=[NSArray arrayWithObjects:#"111",#"222",#"333",#"444",#"555", nil];
}
return self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// Returns
switch (component) {
case 0: return [notesToDisplayForKey count];
case 1:
if([selectedKey isEqualToString:#"UK" ])
{
return [UKsize1 count];
}
else if([selectedKey isEqualToString:#"US"] )
{
return [USsize3 count];
}
else
{
return [EUsize2 count];
}
//return [scaleNames count];
case 2: return [scaleValue count];
default:break;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0: return [notesToDisplayForKey objectAtIndex:row];
case 1:if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [scaleNames objectAtIndex:row];
return selectedScale;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return selectedScale;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return selectedScale;
}
case 2: return [scaleValue objectAtIndex:row];
default:break;
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(#"Row %i selected in component %i", row, component);
switch (component) {
case 0:
selectedKey = [notesToDisplayForKey objectAtIndex:row];
return;
case 1:
// selectedScale = [scaleNames objectAtIndex:row];
if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [scaleNames objectAtIndex:row];
return;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return;
}
return;
case 2:
selectedValue = [scaleValue objectAtIndex:row];
return;
default:break;
}
}
just check this method .it will work .
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(#"Row %i selected in component %i", row, component);
switch (component) {
case 0:
selectedKey = [notesToDisplayForKey objectAtIndex:row];
[pickerView reloadAllComponents];
return;
case 1:
// selectedScale = [scaleNames objectAtIndex:row];
if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [UKsize1 objectAtIndex:row];
return;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return;
}
return;
case 2:
selectedValue = [scaleValue objectAtIndex:row];
return;
default:break;
}
}
It seems like you are not reloading the picker....Every time you need to change the data in the picker you need to reload the picker just like UITableView.
// Reloading whole view or single component
- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;
I'm trying to set two UIPickerViews, both are filled with NSDictionary, this work ok, but I need to the second UIPickerView implement a refresh depends of the selection of the first UIPickerView, understand me?
The first UIPickerView is filled this way:
for (NSDictionary *local in json) {
NSString *nombre = [local objectForKey:#"name"];
NSString *idLocal = [local objectForKey:#"id"];
self.dicLocales = [NSDictionary dictionaryWithObjectsAndKeys: idLocal, #"idLocal", nombre, #"nombreLocal", nil];
[listaLocales addObject:self.dicLocales];
}
And the second UIPickerView is filled this way:
for (NSDictionary *servicio in json) {
NSString *nombre = [servicio objectForKey:#"name"];
NSString *idServicio = [servicio objectForKey:#"id"];
self.dicServicios = [NSDictionary dictionaryWithObjectsAndKeys: idServicio, #"idServicio", nombre, #"nombreServicio", idLocal, #"idLocal", nil];
[listaServicios1 addObject:self.dicServicios];
}
You note variable idLocal, that corresponds to id of the first UIPickerView rows. Then, I need, if in the first UIPickerView is selected row with idLocal 1, in the second UIPickerView is reloaded or refreshing only with rows corresponds to idLocal 1.
Please hope somebody can help me, and sorry for my English :P
you have to implement <UIPickerViewDelegate> Method as below
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView isEqual:FirstPickerView]) {
//SecondPickerView fill code
[SecondPickerView reloadAllComponents];
}
else {
//SecondPickerView Selected row
}
}
and change your second UIPickerView fill Loop accordingly
I would recommend using (NSInteger)component 0 is the first row and so on.
if (component == 0) {
if (row == 0) {
}
else if (row == 1)
{
}
}
else if (component == 1)
{
selectedPositionIndex = row;
if (row == 0) {
}
else if (row == 1)
{
}
else if (row == 2)
{
}
else if (row == 3)
{
}
}
This code works for just about all the picker view methods that provide the component NSInteger.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
You will need to make sure you return the right item count for each component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return wordArray.count;
}
else if (component == 1)
{
return positionArray.count;
}
return 0;
}
I am using two UIPickerView in the same ViewController. I need to load two different lists of data in to those UIPickerViews. But I can load only for uipickerView1 not for UIPickerView2
-(void)viewDidLoad{
pickerView1.delegate=self;
pickerView2.delegate=self;
pickerView1.tag=1;
pickerView2.tag=2;
// Here I've loaded dataArray1 and dataArray2
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if(pickerView.tag==1)
{
return [dataArray1 count];
}
else if(pickerView.tag==1)
{
return [dataArray2 count];
}
return 1;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
if(row>0)
{
test1.text=[dataArray1 objectAtIndex:row];
}else
{
test1.text=#"";
}
}
else if(pickerView.tag==2)
{
if(row>0)
{
test2.text=[dataArray2 objectAtIndex:row];
}
else{
test2.text=#"";
}
}
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString* title=#"";
if(pickerView.tag==1)
{
title= [dataArray1 objectAtIndex:row];
}
else if (pickerView.tag==2)
{
title= [dataArray2 objectAtIndex:row];
}
return title;
}
I've checked dataArray1 and dataArray2. Both are having 5 datas. But when viewController is loaded, only first picker loads all the data. For second one, only one data, which is in row index zero, has loaded. What is the mistake?
There is problem in your numberOfRowsInComponent method
you are comparing only pickerView.tag==1 in both condition
make change it to
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(pickerView.tag==1)
{
return [dataArray1 count];
}
else if(pickerView.tag==2)
{
return [dataArray2 count];
}
return 1;
}
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}