gpt4 book ai didi

ios - 来自 rightView 的 UITextField 引用

转载 作者:行者123 更新时间:2023-11-29 01:56:27 26 4
gpt4 key购买 nike

我正在将 UIButton 设置为 UITextFieldrightView。此外,我正在设置通过

将目标操作添加到 UIButton
myButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)

我想知道如何从指定为 buttonPressed 的方法访问 UITextField,其方法签名如下所示:

func buttonPressed(sender: UIButton) {

}

我试过了

sender.superview as! UITextField

但我似乎得到了一个nil

此外,我无法使用标签,因为它已被用于其他目的。

编辑:

有关详细信息,myButton.addTarget() 发生在 UITableViewCell 内,这就是为什么我没有 outlet View Controller 中的 UITextField

有人有什么想法吗?

最佳答案

你的代码是正确的

我尝试了以下方法,它对我有用

自定义 textField :

    var textFiled = UITextField(frame: CGRectMake(20.0, 50.0, 100.0, 33.0))
textFiled.borderStyle = UITextBorderStyle.RoundedRect

let purpleImageButton = UIButton(frame :CGRectMake(0, 0, 10, 30))
purpleImageButton.backgroundColor = UIColor.blackColor()
purpleImageButton.setTitle("T", forState: UIControlState.Normal)
purpleImageButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
textFiled.rightViewMode = UITextFieldViewMode.Always
textFiled.rightView = purpleImageButton
self.view.addSubview(textFiled)

函数 buttonPressed :

func buttonPressed(sender: UIButton) {
let textF : UITextField = sender.superview as UITextField
textF.text = "Hello"
}

点击前

enter image description here

点击 textField 按钮后:

enter image description here

希望您能找到问题所在。

这是 Xcode 6.1 版。因此,我的示例将按照 Swift 1.1。

关于ios - 来自 rightView 的 UITextField 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30797419/

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