1

我是 Objective-C 的新手,我需要在触摸特定文本字段时创建一个 UIPickerView,该文本字段将填充 NSMutableArray 的项目。我已经实现了这段代码,但看不到添加的 pickerView。我仍然可以看到键盘触摸文本字段时弹出...我哪里错了..???

- (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];
 }  

我的数组看起来像这样:

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

任何帮助,将不胜感激...

4

2 回答 2

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

确保您的按钮应该是自定义类型的按钮,并将该按钮放在文本字段上。

于 2013-06-05T07:07:45.167 回答
0

如果你不给委托人UITextField命名,那么在方法txtstate中像下面这样给委托人viewDidLoad:

txtstate.delegate = self;
于 2012-11-09T07:19:55.250 回答