如何根据 UIPickerview 选择运行代码?
Posted
技术标签:
【中文标题】如何根据 UIPickerview 选择运行代码?【英文标题】:How to run code based on UIPickerview selections? 【发布时间】:2020-06-02 14:09:24 【问题描述】:我正在制作一个类似于谷歌文档或其他文本编辑器的简单 ios 应用程序。我创建了一个 UIPickerview 来更改字体大小,但后来我意识到当我选择某些东西时,什么也没有发生。那么有什么办法可以做到这一点,这是我的代码
import UIKit
class FirstViewController: UIViewController, UIPopoverPresentationControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var fontBTN: UIButton!
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return arrayFruits.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return arrayFruits[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
var arrayFruits = [String]()
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var aligners: UISegmentedControl!
@IBOutlet weak var foramtters: UISegmentedControl!
@IBOutlet weak var textView: UITextView!
var pickerData = ["10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "36", "48", "72"]
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
self.picker.delegate = self
self.picker.dataSource = self
arrayFruits = ["10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "36", "48", "72"]
@IBAction func aligning(_ sender: Any)
switch aligners.selectedSegmentIndex
case 0:
textView.textAlignment = .left
case 1:
textView.textAlignment = .center
case 2:
textView.textAlignment = .right
case 3:
textView.textAlignment = .justified
default: break
@IBAction func formating(_ sender: Any)
switch foramtters.selectedSegmentIndex
case 0:
textView.font = UIFont.boldSystemFont(ofSize: 14)
case 1:
textView.attributedText = NSAttributedString(string: textView.text, attributes:
[.underlineStyle: NSUnderlineStyle.single.rawValue])
textView.font = UIFont(name: "Arial", size: 14)
case 2:
textView.font = UIFont.italicSystemFont(ofSize: 14)
default: break
@IBAction func fontPicker(_ sender: Any)
if picker.isHidden == true
picker.isHidden = false
fontBTN.setTitle("Done", for: .normal)
else if picker.isHidden == false
fontBTN.setTitle("Aa", for: .normal)
picker.isHidden = true
我尝试过Switch statement
,但我收到错误消息"Expression pattern of type 'Int' cannot match values of type 'UIPickerView?'"
我尝试用谷歌搜索,但没有任何显示,所以我问了这个问题。
switch picker
case 0:
//funtion here
default:
你能帮帮我吗?
【问题讨论】:
didSelectRow
的主体是空的,所以当您在选择器中选择项目时它什么也不做,将您想要的代码移到那里。
我要在那里放一个 switch 语句吗?
你到底想达到什么目的?
更改文本视图的字体大小
【参考方案1】:
考虑以下代码:
class ViewController: UIViewController
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var textView: UITextView!
let data: [CGFloat] = [20, 25, 30]
override func viewDidLoad()
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
extension ViewController: UIPickerViewDelegate
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
textView.font = UIFont.systemFont(ofSize: data[row])
extension ViewController: UIPickerViewDataSource
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return data.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return "\(data[row])"
因此,您可以通过实现如上所示的 didSelectRow
方法来运行基于 UIPickerView 选择的代码。编辑:
我已更改代码以更新 textView 的 fontSize。
【讨论】:
【参考方案2】:要根据选择器选择更改 textView 的字体大小,您需要执行以下操作。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if let doubleValue = Double(arrayFruits[row])
let fontSize = CGFloat(doubleValue)
textView.font = UIFont.systemFont(ofSize: fontSize)
【讨论】:
以上是关于如何根据 UIPickerview 选择运行代码?的主要内容,如果未能解决你的问题,请参考以下文章
如何根据按钮按下重新加载/刷新 UIPickerView(带有新数据数组)?
无法根据来自 UIPickerView 的选择器更新 UITableView 数据
如何在 UIPickerView 中获取用户选择并在其他地方使用它?