【发布时间】:2015-08-05 07:10:15
【问题描述】:
在文本字段中显示键值。
这就是我获取数据的方式:
-(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;
}
}
这是显示数据的方式:
-(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 视图但不显示标题名称只显示'{'符号。
然后滚动它来设置数组键和值的值,但我只是设置值。
uitextfield setvalue:
{ "violation_name" = "Street Light"; }
and
uipickerview display:
{
{
{
then to select any value to set in text field.
uipickerview 执行选择文本字段而不使用任何按钮
【问题讨论】:
-
你的编码很好,我认为你的数组包含另一个索引,所以试试 txtviolation.text =[[ViolationA objectAtIndex:row]objectatindex:0];,实际上我们不知道你的 ViolationA 包含
-
@Nilesh:你的代码一点也不干净,不必要重复相同的代码...请通过此链接stackoverflow.com/a/11612702/1570609
-
@property(strong, nonatomic) NSMutableArray *ViolationA; @property(strong, nonatomic) NSMutableArray *VTypeA;
标签: ios uitextfield nsmutablearray uipickerview