1

display the key value in textfield.

this is how I am fetching data:

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

    if (txtviolation.editing == YES)
    {
        pickerV = [[UIPickerView alloc]init];
        pickerV.dataSource = self;[![enter image description here][1]][1]
        pickerV.delegate = self;
        pickerV.showsSelectionIndicator = YES;
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];

        [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
        [toolBar setBarStyle:UIBarStyleBlackOpaque];
        NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
        [toolBar setItems:toolbarItems];
        txtviolation.inputView = pickerV;
        txtviolation.inputAccessoryView = toolBar;
        flag=1;
    }
    else if (txttype.editing == YES)
    {

        pickerV = [[UIPickerView alloc]init];
        pickerV.dataSource = self;
        pickerV.delegate = self;
        pickerV.showsSelectionIndicator = YES;
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];

        [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
        [toolBar setBarStyle:UIBarStyleBlackOpaque];
        NSArray *toolbarItems = [NSArray arrayWithObjects:
                                 doneButton, nil];
        [toolBar setItems:toolbarItems];
        txttype.inputView = pickerV;
        txttype.inputAccessoryView = toolBar;
        flag=2;
    }
}

this is how display data:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (flag==1)
    {
        return ViolationA.count;
    }
    else if (flag==2)
    {
        return VTypeA.count;
    }
    return 0;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (flag==1)
    {
        return [NSString stringWithFormat:@"%@", [ViolationA objectAtIndex:row]];
    }
    else if (flag==2)
    {
        return [NSString stringWithFormat:@"%@", [VTypeA objectAtIndex:row]];
    }

    return 0;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (flag==1)
    {
      //  [txtviolation setText:[[ViolationA objectAtIndex:row] forKeyPath:@"violation_name"]];

        txtviolation.text =[NSString stringWithFormat:@"%@", [ViolationA objectAtIndex:row]];
    }
    else if (flag==2)
    {
        txttype.text = [NSString stringWithFormat:@"%@", [VTypeA objectAtIndex:row]];

    }
}

I am not able to bind:

click on textfield then open the uipicker view but not display title name only display the '{' sign.

then roll it to set value of array key and value also but I am only set the value.

uitextfield setvalue:
 {   "violation_name" = "Street Light";  }

and

uipickerview display:
  {
  {
  {

then to select any value to set in text field.

uipickerview perform to select the text field not use any button

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nilesh
  • 104
  • 9
  • your coding is fine , i think you array contains another one index so try txtviolation.text =[[ViolationA objectAtIndex:row]objectatindex:0];, actually we don't know whats your **ViolationA** contains – Anbu.Karthik Aug 05 '15 at 07:15
  • @Nilesh: your code not at all clean, unnecessary repeat the same code...Please go through this link http://stackoverflow.com/a/11612702/1570609 – Adarsh G J Aug 05 '15 at 08:08
  • @property(strong, nonatomic) NSMutableArray *ViolationA; @property(strong, nonatomic) NSMutableArray *VTypeA; – Nilesh Aug 05 '15 at 08:42

1 Answers1

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (flag==0)
    {
        return ViolationA.count;
    }
    else if (flag==1)
    {
        return VTypeA.count;
    }
    return 0;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (flag==0)
    {
        return [[ViolationA objectAtIndex:row] valueForKey:@"violation_name"];
    }
    else if (flag==1)
    {
        return [[VTypeA objectAtIndex:row] valueForKey:@"priorirty_name"];
        //[NSString stringWithFormat:@"%@", [VTypeA objectAtIndex:row]];
        //violation_name
    }
    return 0;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (flag==0)
    {
        txtviolation.text =[[ViolationA objectAtIndex:row] valueForKey:@"violation_name"];
        //[NSString stringWithFormat:@"%@",[ViolationA objectAtIndex:row]];
    }
    else if (flag==1)
    {
        txttype.text = [[VTypeA objectAtIndex:row] valueForKey:@"priorirty_name"];

    }
}


-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    pickerV = [[UIPickerView alloc]init];
    pickerV.dataSource = self;
    pickerV.delegate = self;
    pickerV.showsSelectionIndicator = YES;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
    [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
    [toolBar setBarStyle:UIBarStyleBlackOpaque];
    NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
    [toolBar setItems:toolbarItems];

    if (txtviolation.editing == YES)
    {
        txtviolation.inputView = pickerV;
        txtviolation.inputAccessoryView = toolBar;
        flag=0;
    }
    else if (txttype.editing == YES)
    {
        txttype.inputView = pickerV;
        txttype.inputAccessoryView = toolBar;
        flag=1;
    }
}
Nilesh
  • 104
  • 9