简体   繁体   中英

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..???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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