【发布时间】:2012-01-30 13:09:13
【问题描述】:
我有一个表格视图,我必须在某些单元格中显示文本字段。当用户单击文本字段时,我希望选择器视图显示从 0 到 99 的整数值。一旦用户在选择器视图中选择项目,则需要在该特定文本字段上显示相同的项目。任何帮助将不胜感激。
【问题讨论】:
-
你能告诉我你尝试了什么吗?
标签: iphone objective-c ios cocoa-touch uikit
我有一个表格视图,我必须在某些单元格中显示文本字段。当用户单击文本字段时,我希望选择器视图显示从 0 到 99 的整数值。一旦用户在选择器视图中选择项目,则需要在该特定文本字段上显示相同的项目。任何帮助将不胜感激。
【问题讨论】:
标签: iphone objective-c ios cocoa-touch uikit
-(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;
}
【讨论】:
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
}
【讨论】:
创建一个选择器视图并在您的视图控制器中实现委托和数据源方法。将选取器视图设置为文本字段的inputview。当用户编辑文本字段时,它将弹出而不是键盘。
当用户更改选择器时,使用didSelect... 委托方法更新文本字段值。
【讨论】:
首先在 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 源的数组。
【讨论】: