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}
Related
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];
}
}
Hi I have one picker view. This picker view from web services came to
loading data. This picker view is 2 part. Picker view 1. part is
Project name. and Picker view 2. part is Project number. I want to
write on my label same picker view is 1. part I want to write on label
"A1 Unitesi", and i want to write on label2 "002" but my label on
write "DevamEdenProjelerObje:"0xada6fe0">why on my label write
"DevamEdenProjelerObje:"0xada6fe0"> ?` Can you help to me ? How i can
on my label write "A1 Unitesi" ?
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [RaporlarList count];
}
return [RaporlarList count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeAdii;
}
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeNoo;
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [RaporlarList objectAtIndex:row]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [RaporlarList objectAtIndex:row]];
lblProjeNo.text = resultString;
}
}
I didn't know if I understood well but isn't that better?
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [[RaporlarList objectAtIndex:row] ProjeAdii]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [[RaporlarList objectAtIndex:row] ProjeNoo]];
lblProjeNo.text = resultString;
}
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 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;
}
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;
}
}