ios - 单击按钮,使选择器 View 出现,然后选项出现在标签中

标签 ios swift button pickerview

我已将 View Controller 设置为具有 5 个带有 5 个标签的按钮和一个选择器 View 。我希望将其设置为单击每个按钮后,它将根据选择的按钮拉起选取器 View ,然后选择的选项将显示在按钮旁边的标签中。

为了解决这个问题,我所做的就是我将所有按钮和标签都通过 io socket 连接到我的 swift 文件上,然后我真的不知道之后还能做什么。

我研究过是否有其他指南可以连接按钮+pickerview+标签的所有三个变量,但没有一个指南使用按钮作为触发器。

import UIKit

class CalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var firstButton: UIButton!
    @IBOutlet weak var secondButton: UIButton!
    @IBOutlet weak var thirdButton: UIButton!
    @IBOutlet weak var fourthButton: UIButton!
    @IBOutlet weak var fifthButton: UIButton!

    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondTextField: UITextField!
    @IBOutlet weak var thirdTextField: UITextField!
    @IBOutlet weak var fourthTextField: UITextField!
    @IBOutlet weak var fifthTextField: UITextField!

    @IBOutlet weak var pickerView: UIPickerView!

    var lastPressedButton: UIButton

    override func viewDidLoad() {
        super.viewDidLoad()

        firstButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        secondButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        thirdButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        fourthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        fifthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)

        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
    }

    func buttonAction(sender:UIButton!) {

        lastPressedButton = sender

        if lastPressedButton == firstButton {
            firstTextField.inputView = pickerView
        } else if lastPressedButton == secondButton {
            secondTextField.inputView = pickerView
        }
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if lastPressedButton == firstButton {
            return firstButtonDataSource.count
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource.count
        }
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

        if lastPressedButton == firstButton {
            return firstButtonDataSource[row]
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource[row]
        }
    }

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

        if lastPressedButton == first {
            self.firstTextField.text = firstButtonDataSource[row]
        } else if lastPressedButton == secondButton {
            self.secondTextField.text = firstButtonDataSource[row]
        }
    }
}

最佳答案

我认为您正在寻找类似于以下内容的内容:

更新:

class CalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var firstButton: UIButton!
    @IBOutlet weak var secondButton: UIButton!
    @IBOutlet weak var thirdButton: UIButton!
    @IBOutlet weak var fourthButton: UIButton!
    @IBOutlet weak var fifthButton: UIButton!

    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondTextField: UITextField!
    @IBOutlet weak var thirdTextField: UITextField!
    @IBOutlet weak var fourthTextField: UITextField!
    @IBOutlet weak var fifthTextField: UITextField!

    @IBOutlet weak var pickerView: UIPickerView!

    var firstButtonDataSource = ["1", "2", "3", "4"];
    var secondButtonDataSource = ["White", "Red", "Green", "Blue"];
    var thirdButtonDataSource = ["Mike", "Steve", "Ben", "Peter"];
    var fourthButtonDataSource = ["Large", "Medium", "Small", "Extra-small"];
    var fithButtonDataSource = ["USA", "UK", "France", "Germany"];

    var lastPressedButton: UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()

        firstButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
        secondButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
        thirdButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
        fourthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
        fifthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)

        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
    }

    @objc func buttonClicked(sender:UIButton!) {

        lastPressedButton = sender

        if lastPressedButton == firstButton {
            firstTextField.inputView = pickerView
        } else if lastPressedButton == secondButton {
            secondTextField.inputView = pickerView
        } else if lastPressedButton == thirdButton {
            thirdTextField.inputView = pickerView
        } else if lastPressedButton == fourthButton {
            fourthTextField.inputView = pickerView
        } else if lastPressedButton == fifthButton {
            fifthTextField.inputView = pickerView
        }
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if lastPressedButton == firstButton {
            return firstButtonDataSource.count;
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource.count;
        } else if lastPressedButton == thirdButton {
            return thirdButtonDataSource.count;
        } else if lastPressedButton == fourthButton {
            return fourthButtonDataSource.count;
        } else if lastPressedButton == fifthButton {
            return fithButtonDataSource.count;
        }

        return 0
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if lastPressedButton == firstButton {
            return firstButtonDataSource[row];
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource[row];
        } else if lastPressedButton == thirdButton {
            return thirdButtonDataSource[row];
        } else if lastPressedButton == fourthButton {
            return fourthButtonDataSource[row];
        } else if lastPressedButton == fifthButton {
            return fithButtonDataSource[row];
        }

        return ""
    }

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

        if lastPressedButton == firstButton {
            self.firstTextField.text = firstButtonDataSource[row]
        } else if lastPressedButton == secondButton {
            self.secondTextField.text = secondButtonDataSource[row]
        } else if lastPressedButton == thirdButton {
            self.thirdTextField.text = thirdButtonDataSource[row]
        } else if lastPressedButton == fourthButton {
            self.fourthTextField.text = fourthButtonDataSource[row]
        } else if lastPressedButton == fifthButton {
            self.fifthTextField.text = fithButtonDataSource[row]
        }
    }
}

注意:我在 Playground 上做了这个,所以它没有经过适当的测试,但它应该为你指明正确的方向(希望如此)

关于ios - 单击按钮,使选择器 View 出现,然后选项出现在标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48585874/

相关文章:

ios - CFNetwork SSLHandshake 失败 (-9806) - xcode

ios - MessageKit 头像加载问题

ios - 获取 iOS 上导航栏的默认高度(或最大值)

html - 使用 css id 为按钮设置悬停背景颜色

javascript - HTML 按钮不与 Javascript 交互

ios - viewForFooterInSection 未显示在屏幕底部

ios - CSS - iPhone 6 上未应用高度

ios - 无法取消执行 block 的NSTimer

ios - 从 Swift 中的另一个类访问一些值

ios - 如何用swift连接mysql?