1

I m new to objective-C and I need to create a UIPickerView when a particular textfield is touched that would be populated with the items of NSMutableArray..I have implemented this code but couldnot see the pickerView addded up.I can still see the keyboard popping up when a textfield is touched...Where Am I gng wrong..???

- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {

    if( textField == txtstate)
    {
        [txtstate resignFirstResponder];
        pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];

        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];

    }

    }
  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
   {
return 1;
   }

  - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  {
 [pickerView selectRow:1 inComponent:0 animated:NO];

txtstate.text= [[arr objectAtIndex:row] valueForKey:@"Code"]; 
  }

 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
  {
return [arr count];
  }

  - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
 {

 return [arr objectAtIndex:row];
 }  

My Array looks like this :

(
{
        Code = Wisconsin;
        Id = "6215b339-2959-4a3d-9430-f5bddb0a4eb9";
    },
        {
        Code = Wyoming;
       Id = "7972f963-9b3b-4ba6-a30c-d68bb9f59145";
    }
)

Any help would be appreciated...

Honey
  • 2,840
  • 11
  • 37
  • 71

2 Answers2

2
u can add button on UITextField and add your picker at that button action .

make sure your button should be custom type button and put that button over text field.

Latika Tiwari
  • 306
  • 3
  • 7
0

if you not give the delegate to UITextField name txtstate then give the delegate like bellow in viewDidLoad: method

txtstate.delegate = self;
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • I m getting error "Program received signal SIGABRT".Actually my array looks like this :( { Code = Wisconsin; Id = "6215b339-2959-4a3d-9430-f5bddb0a4eb9"; }, { Code = Wyoming; Id = "7972f963-9b3b-4ba6-a30c-d68bb9f59145"; } ) Now I want the code to be seen in a row and Id also to be added to UIPicker but to be hidden..How can I do it? – Honey Nov 09 '12 at 07:39
  • ohh mate here give the valueForKey attribute dude.. like this txtstate.text= [[arr objectAtIndex:indexPath.row]valueForKey:@"Code"]; :) – Paras Joshi Nov 09 '12 at 07:41
  • @arizah try with this code and then post comment if any error occured dude.. :) – Paras Joshi Nov 09 '12 at 07:42
  • Now Im getting error "Use of undeclared identifier indexPath".I have written it in this event - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { txtstate.text= [[arr objectAtIndex:indexPath.row] valueForKey:@"Code"]; } – Honey Nov 09 '12 at 07:47
  • What I want is only the Code to be seen and Id to be hidden in each row Bcoz based on that selected rows Id I have to do some other task. – Honey Nov 09 '12 at 07:49
  • @arizah oh sorry met use row instead of indexPath.row sorry dear – Paras Joshi Nov 09 '12 at 07:50
  • Still gettting the same error at this event - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; { return 1;//error }.I have updated my code .Can u please have a look and rectify .Actually I want both the values Code and Id to be added to each row but Code should be seen and Id should be hidden – Honey Nov 09 '12 at 07:58
  • @arizah what you want the title on pickerview?? – Paras Joshi Nov 09 '12 at 08:13
  • 1
    and in pickerview title write this same as above dude.. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; { return [[arr objectAtIndex:row] valueForKey:@"Code"]; } – Paras Joshi Nov 09 '12 at 08:14
  • and remove this line mate.. [pickerView selectRow:1 inComponent:0 animated:NO]; – Paras Joshi Nov 09 '12 at 08:26
  • Actually Im using it in a UIScrollView .So trying to scroll the picker ends up scrolling the scroll view itself.and also I want the Id value to be added to each row but hidden (not seen)..How can i do it ? – Honey Nov 09 '12 at 09:02
  • whats your id value type??means string type or any other?? if string type then merge like this NSString *titleStr = [NSString stringWithFormat:@"%@,%@",[[arr objectAtIndex:row] valueForKey:@"Code"],[[arr objectAtIndex:row] valueForKey:@"Id"]]; – Paras Joshi Nov 09 '12 at 09:06
  • It is a dictionary value in array like Code...I mean I have to add Id to each row but make it invisible and in which event should I write it ?Here hidden is very important ...? – Honey Nov 09 '12 at 09:13
  • yes why id displayed to user?? here only code displays is important here not display the id mate .. just display Code title and it is Dictionary?? not an array?? – Paras Joshi Nov 09 '12 at 09:19
  • Actually it is array only.But I have Code and Id values in each index of the array...And I will not display Id to the user so I want it to be hidden.But I want Id bcoz based on that selected rows Id I have to do some further tasks like calling a service url .So I want Id in each row and If I select a particular row Code will be displayed and that selected rows Id I will pass to another variavle and call in a service url.this is the concept – Honey Nov 09 '12 at 09:29
  • ok mate if you have two array or one dict anything else , just see when raw select from didPickerView method at that time just call service with you id array like this [[arr objectAtIndex:row] valueForKey:@"id"]; – Paras Joshi Nov 09 '12 at 09:34
  • ya but Id should be attached to each row first like passing it to some label and making it hidden and later using that selected rows label value ..thats what I m asking – Honey Nov 09 '12 at 09:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19333/discussion-between-paras-joshi-and-arizah) – Paras Joshi Nov 09 '12 at 09:49