【问题标题】:Adding UIPicker to textfield将 UIPicker 添加到文本字段
【发布时间】:2012-01-30 13:09:13
【问题描述】:

我有一个表格视图,我必须在某些单元格中显示文本字段。当用户单击文本字段时,我希望选择器视图显示从 0 到 99 的整数值。一旦用户在选择器视图中选择项目,则需要在该特定文本字段上显示相同的项目。任何帮助将不胜感激。

【问题讨论】:

  • 你能告诉我你尝试了什么吗?

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


【解决方案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;
     }

【讨论】:

  • 谢谢,代码工作正常,但有一个小问题,当我多次单击文本字段时,选择器视图不会在一次选择时关闭,而是在选择输入后会关闭到我点击了很多次文本字段。
  • 还有一件事,当用户选择输入时,我们可以在标题上设置一个完成按钮来关闭选择器视图。
  • 好的,谢谢我设法解决了这两个问题,感谢您给我的指导:)
  • 对不起。只是我看到你的评论。不要将其简单的添加完成按钮添加到pickerview。
【解决方案2】:

Swift iOS:(注意:请根据需要更改变量名称)

第 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
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 在这种情况下,选择器视图位于键盘的位置,但我想在我的文本字段下方
    • 您的意思是选择器视图应该始终可见?
    • 当我将选择器视图设置为文本字段的输入视图时,我的选择器视图位于键盘的位置,但我希望我的选择器视图显示在我的文本字段后面
    【解决方案4】:

    首先在 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 源的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      相关资源
      最近更新 更多