I have a table view & I have to display text fields in some cells. When user click on the text field, I want the picker view to be displayed with integer values from 0 to 99. once the user selects the item in picker view the same needs to be displayed on that particular text field. Any help would be sincerely appreciated.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
-(void) viewDidLoad
{
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@"1"];
[arrayNo addObject:@"2"];
[arrayNo addObject:@"3"];
// and so on....
}
Add target of this line to ur textfield function
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;
}
回答2:
Swift iOS: (Note: please change vars name by requirement)
Step 1 : declare vars and objects
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)
Step 2: declare delegates and protocol:
// 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:
Create a picker view and implement the delegate and datasource methods in your view controller. Set the picker view as the inputview
of your text field. It will then pop up instead of the keyboard when the user edits the text field.
Use the didSelect...
delegate methods to update the text field value when the user alters the picker.
回答4:
First in the viewDidLoad method you need to instantiate the PickerView and to add it to the textField
UIPickerView *pickerView =[[UIPickerView alloc]init];
pickerView.delegate=self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator=YES;
myTextField.inputView=pickerView;
Then you have to implement the UIPickerView delegate
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
myTextField.text=[arrayWithData objectAtIndex:row];
}
Where arrayWithData is the array with the source for the PickerView.