将UIPicker添加到文本字段

时间:2012-01-30 13:09:14

标签: iphone objective-c ios cocoa-touch uikit

我有一张桌子视图&我必须在某些单元格中显示文本字段。当用户单击文本字段时,我希望选择器视图显示为0到99之间的整数值。一旦用户选择了选择器视图中的项目,则需要在该特定文本字段上显示相同的项目。任何帮助都将深表感谢。

4 个答案:

答案 0 :(得分:1)

   -(void) viewDidLoad
 {
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@"1"];
[arrayNo addObject:@"2"];
[arrayNo addObject:@"3"];
// and so on....
  }

将此行的目标添加到您的文本字段功能

  UITextfield *entered2;
  //..... some codes
   [entered2 addTarget:self action:@selector(showPicker)forControlEvents:UIControlEventEditingDidBegin];
[myview addSubview:entered2];




  -(void) showPicker
  {
[entered2 resignFirstResponder];
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,240,220,0)];

pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;    

[self.view addSubview:pickerView];  
[pickerView release]; 
 }

 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
   {
    return [arrayNo count];
   }
  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;  
   }
   - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
   {
entered2.text=[arrayNo objectAtIndex:row];
[pickerView removeFromSuperview];
   }
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
   {
return [arrayNo objectAtIndex:row];
[pickerView removeFromSuperview];
   }
   - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
     }

答案 1 :(得分:1)

Swift iOS :(注意:请按要求更改vars名称)

第1步:声明变量和对象

var objPickerView  : UIPickerView = UIPickerView()

// select the picker view data values
objPickerView.delegate=self;
objPickerView.dataSource=self;
objPickerView.showsSelectionIndicator=true;

// show picker view when text field clicked   
self.smokingStatusText!.inputView = objPickerView


// reload the component of picker
self.objPickerView.reloadAllComponents()

// select the value of picker status(Which row must be selected)
self.objPickerView.selectRow(sStatus!, inComponent: PickerComponent.size.rawValue, animated: false)

第2步:声明代理和协议:

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

//MARK: UIPickerView Delegates

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    smokingStatusText!.text = pickerData[row]
    self.smokerStatusNum = String(row)

}

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let titleData = pickerData[row]

    let font:UIFont? = UIFont(name: "Georgia", size: 14.0)

    let attrString = NSAttributedString(
        string: titleData,
        attributes: NSDictionary(
            object: font!,
            forKey: NSFontAttributeName))

    return attrString
}

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----

//size the components of the UIPickerView
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 36.0
}

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    return 300
}

答案 2 :(得分:0)

创建一个选择器视图,并在视图控制器中实现委托和数据源方法。将选择器视图设置为文本字段的inputview。然后,当用户编辑文本字段时,它将弹出而不是键盘。

当用户更改选择器时,使用didSelect...委托方法更新文本字段值。

答案 3 :(得分:0)

首先在viewDidLoad方法中,您需要实例化PickerView并将其添加到textField

UIPickerView *pickerView =[[UIPickerView alloc]init];
pickerView.delegate=self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator=YES;
myTextField.inputView=pickerView;

然后你必须实现UIPickerView委托

- (void)pickerView:(UIPickerView *)thePickerView 
      didSelectRow:(NSInteger)row 
       inComponent:(NSInteger)component {

    myTextField.text=[arrayWithData objectAtIndex:row];
 }

其中arrayWithData是具有PickerView源的数组。