Ios Can';t不要通过编程方式从下拉按钮中选中按钮标题
我是Swift新手,英语不太好 问题是我在本教程中构建了一个下拉视图 youtube: github: 他以编程的方式创建了一个没有故事板的奇妙的下拉视图 代码在我的Xcode上运行得非常好 但是当我用按钮查看标题时 我从来都不明白 这是我的密码Ios Can';t不要通过编程方式从下拉按钮中选中按钮标题,ios,swift,Ios,Swift,我是Swift新手,英语不太好 问题是我在本教程中构建了一个下拉视图 youtube: github: 他以编程的方式创建了一个没有故事板的奇妙的下拉视图 代码在我的Xcode上运行得非常好 但是当我用按钮查看标题时 我从来都不明白 这是我的密码 class ViewController: UIViewController, GMSMapViewDelegate override func viewDidLoad() super.viewDidLoad()
class ViewController: UIViewController, GMSMapViewDelegate
override func viewDidLoad()
super.viewDidLoad()
let typeBTN = dropDownBtn()
self.view.addSubview(typeBTN)
typeBTN.setTitle("animal", for: .normal)
typeBTN.dropView.dropDownOptions = ["dog", "cat", "cow", "boy"]
// ....
protocol dropDownProtocol {
func dropDownPressed(string: String)
}
class dropDownBtn: UIButton, dropDownProtocol {
func dropDownPressed(string: String) {
self.setTitle(string, for: .normal)
self.dismissDropDown()
}
var dropView = dropDownView()
var height = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.darkGray
dropView = dropDownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
dropView.delegate = self
dropView.translatesAutoresizingMaskIntoConstraints = false
}
override func didMoveToSuperview() {
self.superview?.addSubview(dropView)
self.superview?.bringSubviewToFront(dropView)
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
height = dropView.heightAnchor.constraint(equalToConstant: 0)
}
var isOpen = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isOpen == false {
isOpen = true
NSLayoutConstraint.deactivate([self.height])
if self.dropView.tableView.contentSize.height > 150 {
self.height.constant = 170
} else {
self.height.constant = self.dropView.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
self.dropView.layoutIfNeeded()
self.dropView.center.y += self.dropView.frame.height / 2
}, completion: nil)
}else{ dismissDropDown() }
}
func dismissDropDown() {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class dropDownView: UIView, UITableViewDelegate, UITableViewDataSource {
var dropDownOptions = [String]()
var tableView = UITableView()
var delegate: dropDownProtocol!
override init(frame: CGRect) {
super.init(frame: frame)
tableView.backgroundColor = UIColor.darkGray
tableView.delegate = self
tableView.dataSource = self
self.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dropDownOptions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = dropDownOptions[indexPath.row]
cell.backgroundColor = UIColor.darkGray
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate.dropDownPressed(string: dropDownOptions[indexPath.row])
print(dropDownOptions[indexPath.row])
因为我想这样做
if typeBTN.currentTitle == "dog" {
//show some date at my mapview
}
但当我点击下拉菜单中的“狗”时
这是行不通的
有人能帮我吗?您可以使用委派模式通知视图控制器的标题更改 添加协议:
protocol DropDownButtonDelegate: AnyObject {
func titleDidChange(_ newTitle: String)
}
在DropDownButton类a委托属性中添加:
weak var delegate: DropDownButtonDelegate?
然后在下拉功能中:
func dropDownPressed(string: String) {
self.setTitle(string, for: .normal)
self.dismissDropDown()
delegate?.titleDidChange(string)
}
最后,在视图控制器中实现DropDownButtonDelegate:
class ViewController: UIViewController, GMSMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let typeBTN = dropDownBtn()
self.view.addSubview(typeBTN)
typeBTN.delegate = self
typeBTN.setTitle("animal", for: .normal)
typeBTN.dropView.dropDownOptions = ["dog", "cat", "cow", "boy"]
}
}
extension ViewController: DropDownButtonDelegate {
func titleDidChange(_ newTitle: String) {
print(newTitle)
}
}
您可以使用委派模式通知视图控制器的标题更改 添加协议:
protocol DropDownButtonDelegate: AnyObject {
func titleDidChange(_ newTitle: String)
}
在DropDownButton类a委托属性中添加:
weak var delegate: DropDownButtonDelegate?
然后在下拉功能中:
func dropDownPressed(string: String) {
self.setTitle(string, for: .normal)
self.dismissDropDown()
delegate?.titleDidChange(string)
}
最后,在视图控制器中实现DropDownButtonDelegate:
class ViewController: UIViewController, GMSMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let typeBTN = dropDownBtn()
self.view.addSubview(typeBTN)
typeBTN.delegate = self
typeBTN.setTitle("animal", for: .normal)
typeBTN.dropView.dropDownOptions = ["dog", "cat", "cow", "boy"]
}
}
extension ViewController: DropDownButtonDelegate {
func titleDidChange(_ newTitle: String) {
print(newTitle)
}
}
对不起,我这么做了。但它没有打印新的标题,你能给我更多的点吗?还是再帮我查一下密码?谢谢你,布鲁斯!您是否在ViewController的viewDidLoad函数中添加了
typeBTN.delegate=self
?您好,弗朗西斯科·德里罗。哦~你说得对。我很怀念。现在,当标题更改时,打印(newTitle)是打印标题。我能再问一个问题吗?如果typeBTN.currentTitle==“dog”{//show some date at my mapview}我尝试使用此函数执行如下设置操作,但我不知道如何获取它如果我理解了您的问题,您可以使用newTitle执行相同操作,因为它表示您的typeBTN当前标题,因此:如果newTitle==“dog”
然后执行您的操作。我不会在viewDidLoad中调用newTitle。我错过什么了吗?顺便说一句,我已经投了你的票,但是这是show谢谢你的反馈!声誉低于15的人所投的票将被记录,…
对不起,我这么做了。但它没有打印新的标题,你能给我更多的点吗?还是再帮我查一下密码?谢谢你,布鲁斯!您是否在ViewController的viewDidLoad函数中添加了typeBTN.delegate=self
?您好,弗朗西斯科·德里罗。哦~你说得对。我很怀念。现在,当标题更改时,打印(newTitle)是打印标题。我能再问一个问题吗?如果typeBTN.currentTitle==“dog”{//show some date at my mapview}我尝试使用此函数执行如下设置操作,但我不知道如何获取它如果我理解了您的问题,您可以使用newTitle执行相同操作,因为它表示您的typeBTN当前标题,因此:如果newTitle==“dog”
然后执行您的操作。我不会在viewDidLoad中调用newTitle。我错过什么了吗?顺便说一句,我已经投了你的票,但是这是show谢谢你的反馈!声誉低于15%的人所投的票将被记录,…