关键词:
我是斯威夫特的新人,我的英语不是很好
问题是我从本教程构建了一个下拉视图
youtube:https://youtu.be/22zu-OTS-3M github:https://github.com/Archetapp/Drop-Down-Menu
他以编程方式创建了一个没有故事板的精彩下拉视图
代码在我的Xcode上运行得非常好
但是当我用按钮检查标题时
我从来没有得到它。
这是我的代码
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])
我已经尝试了很多来检查按钮标题何时更改。像这样
print(typeBTN.currentTitle!)
print(typeBTN.titleLabel?.text! )
print(typeBTN.title(for: .normal))
//I get nothing when i pressed dropdown menu options?
因为我想这样做
if typeBTN.currentTitle == "dog"
//show some date at my mapview
但是当我点击下拉菜单的“狗”时
它永远不会奏效
有人能帮助我吗?
您可以使用委派模式通知视图控制器的标题更改。
添加协议:
protocol DropDownButtonDelegate: AnyObject
func titleDidChange(_ newTitle: String)
在DropDownButton类中添加一个委托属性:
weak var delegate: DropDownButtonDelegate?
然后在你的dropDownPressed函数中:
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)
以编程方式禁用/启用通知
】以编程方式禁用/启用通知【英文标题】:Programmaticallydisabling/enablingnotifications【发布时间】:2013-07-1603:14:19【问题描述】:有没有办法以编程方式禁用单个程序的通知?我想建立一个我设备上安装的所有程序的列表,然后检查... 查看详情
Android:以编程方式检测设备是不是有硬件菜单按钮
】Android:以编程方式检测设备是不是有硬件菜单按钮【英文标题】:Android:ProgrammaticallydetectifdevicehashardwaremenubuttonAndroid:以编程方式检测设备是否有硬件菜单按钮【发布时间】:2012-02-2103:03:54【问题描述】:我目前正在努力解... 查看详情
以编程方式创建的后退按钮未显示在导航栏中?
...题描述】:为了简单起见,由于应用程序设计的原因,我不能使用嵌入式导航控制器,而必须使用视图控制器上的手动后退按钮返回到前一个。据我所知,以前的视图控制器也以编程方式推送到这个视图控制器。所以我在这里找... 查看详情
如何以编程方式重置或取消选中 Angular 中的单选按钮?
】如何以编程方式重置或取消选中Angular中的单选按钮?【英文标题】:HowdoIresetorUnchecktheRadiobuttoninAngularprogrammatically?【发布时间】:2021-06-3015:10:54【问题描述】:这是我使用单选按钮选择产品条件的HTML页面。当用户检查New或used... 查看详情
以编程方式设置 Android 按钮样式
】以编程方式设置Android按钮样式【英文标题】:AndroidButtonStylingProgrammatically【发布时间】:2013-01-1719:25:49【问题描述】:如何以编程方式向android按钮添加/删除样式?是否可以在运行时应用样式?我有两个类似这样的按钮---------... 查看详情
以编程方式更改按钮颜色
】以编程方式更改按钮颜色【英文标题】:Changingbuttoncolorprogrammatically【发布时间】:2010-12-2115:36:26【问题描述】:有没有办法以编程方式改变按钮的颜色,或者至少改变按钮标签的颜色?我可以用更改标签本身document.getElementByI... 查看详情
获取 BootstrapVue 下拉菜单(b-dropdown)以在单击按钮时显示
】获取BootstrapVue下拉菜单(b-dropdown)以在单击按钮时显示【英文标题】:GetBootstrapVueDropdown(b-dropdown)toshowwhenclickingabutton【发布时间】:2019-10-0923:52:33【问题描述】:使用Vue.js2.6.10和BootstrapVue2.0.0-rc.20并尝试在单击单个文件组件中... 查看详情
底部的按钮以编程方式
】底部的按钮以编程方式【英文标题】:Buttononthebottomprogrammatically【发布时间】:2017-08-0210:05:38【问题描述】:我正在尝试使用这个库:https://github.com/yannickl/DynamicButton#requirements我需要以编程方式(Swift)将底部放在ViewController的底... 查看详情
以编程方式设置按钮样式
】以编程方式设置按钮样式【英文标题】:Setbuttonstyleprogrammatically【发布时间】:2014-09-2022:04:24【问题描述】:所以我以编程方式创建新按钮并将它们添加到LinearLayout,但是我想用预定义的样式初始化这些按钮。我花了一些时间... 查看详情
以编程方式链接的按钮
】以编程方式链接的按钮【英文标题】:Programmaticallylinkedbuttons【发布时间】:2014-03-1214:46:51【问题描述】:我正在设计一个应用程序,它要求我有一个按钮网格,每个按钮都有四种不同的状态。Unselected、Selected、Hit或Miss。我正... 查看详情
以编程方式将按钮添加到导航栏
】以编程方式将按钮添加到导航栏【英文标题】:Addbuttontonavigationbarprogrammatically【发布时间】:2011-02-2008:11:28【问题描述】:您好,我需要在右侧的导航栏中以编程方式设置按钮,这样如果我按下按钮,我将执行一些操作。我... 查看详情
如何以编程方式单击 WPF 中的按钮?
】如何以编程方式单击WPF中的按钮?【英文标题】:HowtoprogrammaticallyclickabuttoninWPF?【发布时间】:2010-10-1805:23:31【问题描述】:由于WPF中没有button.PerformClick()方法,有没有办法以编程方式单击WPF按钮?【问题讨论】:【参考方案... 查看详情
以编程方式更改按钮的属性
】以编程方式更改按钮的属性【英文标题】:Changepropertyofabuttonprogrammatically【发布时间】:2012-02-0814:47:07【问题描述】:我正在处理另一个人的代码。他在界面构建器的UIView中创建了一些UIButtons(不是以编程方式)。但现在用户... 查看详情
Segues 不能以编程方式工作
】Segues不能以编程方式工作【英文标题】:Seguesnotworkingprogrammatically【发布时间】:2013-08-2218:46:22【问题描述】:根据以下说明,我将一个项目从使用XIB迁移到Storyboard:https://***.com/a/9708723/2604030进展顺利。但是我不能让segues以编... 查看详情
Android,以编程方式布局按钮视图?
】Android,以编程方式布局按钮视图?【英文标题】:Android,programmaticallylayoutabuttonview?【发布时间】:2010-10-2815:43:01【问题描述】:我正在尝试以编程方式定义我的程序布局并在某个位置添加一个按钮。我没有使用布局xml作为内... 查看详情
如何以编程方式创建多个按钮和操作?
】如何以编程方式创建多个按钮和操作?【英文标题】:Howtocreatemultiplebuttonsandactionprogrammatically?【发布时间】:2016-09-1614:57:30【问题描述】:我正在使用Swift3.0。我正在尝试生成按钮的动态网格如何以编程方式为每个生成的按钮... 查看详情
swiftswift-以编程方式添加导航栏标题和按钮(代码片段)
以编程方式的Android按钮位置
】以编程方式的Android按钮位置【英文标题】:AndroidButtonPositionProgrammatically【发布时间】:2011-03-1418:47:53【问题描述】:我的应用程序中有一个按钮。我想以编程方式改变它的位置。我在XML中创建了一个按钮,如下所示:<?xmlve... 查看详情