gpt4 book ai didi

ios - 如何更改 UISegmentedControl 索引更改时显示的内容?

转载 作者:行者123 更新时间:2023-12-01 16:13:31 27 4
gpt4 key购买 nike

我的分段 Controller 有一个小问题,并尝试在单击新索引时更改向用户显示的 View 。我设置了代码以显示用户在更改索引时输入的文本框。一个问题是我想在分段 Controller 下均匀放置文本框,另一个问题是当我单击 Controller 中的新索引时,先前的文本框仍在屏幕上,但我需要它只显示与所选特定索引相关的文本框。代码如下所示,我在其中创建了段 Controller 并处理了索引的切换以及文本框的创建。

    let segmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Inventory", "Sale", "Expense"])
sc.selectedSegmentIndex = 0
sc.layer.cornerRadius = 9
sc.layer.borderWidth = 1
sc.layer.borderColor = UIColor.lightGray.cgColor
sc.layer.masksToBounds = true

sc.addTarget(self, action: #selector(handleSegmentChange), for: .valueChanged)
return sc
}()

//function to handle click of segmented control buttons
@objc fileprivate func handleSegmentChange(sender: UISegmentedControl){
print(segmentedControl.selectedSegmentIndex)

switch segmentedControl.selectedSegmentIndex{
case 0:
view.addSubview(myProduct)
case 1:
view.addSubview(myRetail)
default:
view.addSubview(myExpense)
break
}

}

let myProduct : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 10))
mp.placeholder = "Enter Product name"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan

return mp
}();

let myRetail : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 20))
mp.placeholder = "Enter Retail price"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan

return mp
}();

let myExpense : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 30))
mp.placeholder = "Enter Expense name"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan

return mp
}();

我看到的大多数解决方案都已经过时了,这就是为什么我在这里发布我自己的版本。提前感谢任何人都可以提供的帮助。

最佳答案

您可以设置tag对于每个 UITextField如下,

let myProduct : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 10))
mp.placeholder = "Enter Product name"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan
mp.tag = 11
return mp
}()

let myRetail : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 20))
mp.placeholder = "Enter Retail price"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan
mp.tag = 12
return mp
}()

let myExpense : UITextField = {
let mp = UITextField(frame: CGRect(x: 10, y: 320, width: 300, height: 30))
mp.placeholder = "Enter Expense name"
mp.borderStyle = UITextField.BorderStyle.line
mp.backgroundColor = .white
mp.textColor = .cyan
mp.tag = 13
return mp
}()

然后在 handleSegmentChange 内,在添加新 View 之前删除带有该标签的任何 View ,如下所示,
//function to handle click of segmented control buttons
@objc fileprivate func handleSegmentChange(sender: UISegmentedControl){
print(segmentedControl.selectedSegmentIndex)

[11, 12, 13].forEach { self.view.viewWithTag($0)?.removeFromSuperview() }

switch segmentedControl.selectedSegmentIndex {
case 0:
view.addSubview(myProduct)
case 1:
view.addSubview(myRetail)
default:
view.addSubview(myExpense)
}
}

对于位置问题,您尚未将任何框架设置为 UISegmentControl .您可以设置 frame如下图所示,
let segmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Inventory", "Sale", "Expense"])
sc.selectedSegmentIndex = 0
sc.layer.cornerRadius = 9
sc.layer.borderWidth = 1
sc.layer.borderColor = UIColor.lightGray.cgColor
sc.layer.masksToBounds = true
sc.frame = CGRect(origin: CGPoint(x: 10, y: 280), size: CGSize(width: 300, height: 30))

sc.addTarget(self, action: #selector(handleSegmentChange), for: .valueChanged)
return sc
}()

enter image description here

关于ios - 如何更改 UISegmentedControl 索引更改时显示的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58387041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com