ios:在 uitextfield 中显示数组值?

ios : display the array value in uitextfield?

在文本字段中显示键值。

这就是我获取数据的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-(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;
    }
}

这是显示数据的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-(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]];

    }
}

我无法绑定:

点击文本框然后打开 uipicker 视图但不显示标题名称只显示 '{' 符号。

然后滚动它以设置数组键和值的值,但我只设置值。

1
2
3
4
5
6
7
8
9
10
11
uitextfield setvalue:
 {  "violation_name" ="Street Light";  }

and

uipickerview display:
  {
  {
  {

then to select any value to set in text field.

uipickerview 执行选择文本字段不使用任何按钮


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-(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;
    }
}