【问题标题】:Accessing UIBarButtonItem textfield.text using swift使用 swift 访问 UIBarButtonItem textfield.text
【发布时间】:2015-06-01 22:22:05
【问题描述】:

我在视图底部的工具栏中有一个文本字段,用于发布 cmets。

@IBOutlet var commentText: UIBarButtonItem!

@IBAction func commentButton(sender: AnyObject) {

    println(commentText.text) // dosent work
}

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

【问题讨论】:

  • 你在哪里 UITextfield 我只能看到一个 UIBarButtonItem?
  • 在我的故事板中,我有一个底部有一个工具栏的 tableviewController。我将一个文本字段拖到工具栏中,然后当我制作插座时,它说它是一个 UIBarButtonItem,而不是 uitextfield

标签: ios swift uitextfield uibarbuttonitem


【解决方案1】:

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

所以:

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

【讨论】:

  • 不客气。这是在 Interface Builder(故事板编辑器)中做事的问题:它掩盖了您真正在做什么(从代码的角度来看)。从 Interface Builder 中了解 customView 并不容易!
猜你喜欢
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
相关资源
最近更新 更多