我正在通过制作 iPhone 应用程序来学习 Swift。我遇到的问题涉及实现 UIPickerView。我有一个按钮,如果按下该按钮,应该使 UIPickerView 从屏幕底部上升到主视图上方的设定长度。然后,当用户从 UIPickerView 中选择一个项目时,UIPickerView 会从主视图中下降,直到再次被调用。
我试图复制的一个很好的例子是,当您在时钟应用程序中添加闹钟时,没有视图覆盖所有内容,没有标签,也没有开关。
我还想让我从 PickerView 中选择的项目触发一个函数,但我认为那是在我克服这个障碍之后。
这是我到目前为止的相关代码。
@IBOutlet weak var pickerContainer: UIView!
@IBOutlet weak var pickerView: UIPickerView!
let picker_values = ["val 1", "val 2", "val 3", "val 4"];
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonPressed(sender: AnyObject) {
print("pressing a die!");
}
提前致谢!
好的,你可以使用 UITextField 来做到这一点
步骤01:-
添加 UITextField。选择您的板样式不可见并设置色调颜色白色/透明颜色。
步骤02:-
然后在 UITextField 的顶部添加一个 UIButton,添加相同的宽度高度和约束。这是故事板层次结构,
步骤03:-
然后将 BUTTON 和 TextField 连接到此类函数:-
将按钮连接到这个@IBAction func按钮(sender: AnyObject),并将UITextField连接到@IBAction func textField(sender: UITextField)。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var textFeild: UITextField!
let picker_values = ["val 1", "val 2", "val 3", "val 4"]
var myPicker: UIPickerView! = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
self.myPicker = UIPickerView(frame: CGRectMake(0, 40, 0, 0))
self.textFeild.delegate = self
self.myPicker.delegate = self
self.myPicker.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - Delegates and data sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return picker_values.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return picker_values[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//Your Function
print("Hello")
}
@IBAction func button(sender: AnyObject) {
self.textFeild.becomeFirstResponder()
}
func cancelPicker(sender:UIButton) {
//Remove view when select cancel
self.textFeild.resignFirstResponder() // To resign the inputView on clicking done.
}
@IBAction func textField(sender: UITextField) {
//Create the view
let tintColor: UIColor = UIColor(red: 101.0/255.0, green: 98.0/255.0, blue: 164.0/255.0, alpha: 1.0)
let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))
myPicker.tintColor = tintColor
myPicker.center.x = inputView.center.x
inputView.addSubview(myPicker) // add date picker to UIView
let doneButton = UIButton(frame: CGRectMake(100/2, 0, 100, 50))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.setTitle("Done", forState: UIControlState.Highlighted)
doneButton.setTitleColor(tintColor, forState: UIControlState.Normal)
doneButton.setTitleColor(tintColor, forState: UIControlState.Highlighted)
inputView.addSubview(doneButton) // add Button to UIView
doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event
let cancelButton = UIButton(frame: CGRectMake((self.view.frame.size.width - 3*(100/2)), 0, 100, 50))
cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
cancelButton.setTitle("Cancel", forState: UIControlState.Highlighted)
cancelButton.setTitleColor(tintColor, forState: UIControlState.Normal)
cancelButton.setTitleColor(tintColor, forState: UIControlState.Highlighted)
inputView.addSubview(cancelButton) // add Button to UIView
cancelButton.addTarget(self, action: "cancelPicker:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event
sender.inputView = inputView
}
}
顺便确保您的 UITextField 发送的事件是“编辑已开始”,如下所示:-
希望这对您有帮助...
就是这样!
在视图顶部添加 UITextField
将边框样式设置为“无”,并将清除按钮设置为“从不出现”
将 UITextField 出口附加到视图控制器。
编写以下代码将文本字段的输入视图设置为选择器视图:
- (void)setupPickerView
{
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 225)];
pickerView.backgroundColor = [UIColor colorWithRed:210/255.0 green:212/255.0 blue:217/255.0 alpha:1.0];
pickerView.delegate = self;
pickerView.dataSource = self;
UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleDefault;
toolBar.translucent = YES;
[toolBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(donePicker)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(donePicker)];
[toolBar setItems:@[cancelButton, spaceButton, doneButton] animated:YES];
toolBar.userInteractionEnabled = YES;
self.textField.inputView = pickerView;
self.textField.inputAccessoryView = toolBar;
}
相应地处理 UIPickerView delegate 和 dataSource 方法。