gpt4 book ai didi

ios - 使用 swift 访问 UIBarButtonItem textfield.text

转载 作者:行者123 更新时间:2023-11-28 07:06:06 26 4
gpt4 key购买 nike

我在 View 底部的工具栏中有一个文本字段,可用于发表评论。

@IBOutlet var commentText: UIBarButtonItem!

@IBAction func commentButton(sender: AnyObject) {

println(commentText.text) // dosent work
}

我正在尝试获取 textfield.text,但我不能只说 commentText.text,因为它是一个 UIBarButtonItem。我试过 commentText.target!.text 但没有用。我 2 周前才开始使用 swift,所以我使用 Storyboard来放入项目,而不是以编程方式制作它们

最佳答案

UITextField 是 UIBarButtonItem 的 customView。但即便如此,customView 也只是一个 UIView,因此您必须将其转换为告诉编译器它是一个 UITextField。

所以:

println((commentText.customView as! UITextField).text)

关于ios - 使用 swift 访问 UIBarButtonItem textfield.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584297/

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