简体   繁体   English

iPhone中的UIPickerview?

[英]UIPickerview in iPhone?

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..???我是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];
 }  

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...任何帮助,将不胜感激...

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

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

如果你没有给UITextField名称txtstate的委托,那么在viewDidLoad:方法中像下面那样给委托

txtstate.delegate = self;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM