[英]UITextField reference from rightView
我將UIButton
設置為UITextField
的rightView
。 此外,我正在設置通過以下方式向UIButton
添加目標操作
myButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
我想知道如何從指定為buttonPressed
的方法訪問UITextField
,其方法簽名如下所示:
func buttonPressed(sender: UIButton) {
}
我努力了
sender.superview as! UITextField
但是我似乎nil
。
另外,我無法使用標簽,因為它已用於其他用途。
編輯:
有關更多信息, myButton.addTarget()
發生在UITableViewCell
內部,這就是為什么我在視圖控制器中沒有UITextField
的outlet
。
有人有想法么?
您的密碼是正確的
我嘗試如下,它為我工作
自定義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"
}
點擊之前
單擊textField的按鈕后:
希望你能找到問題所在。
這是Xcode 6.1版。 因此,我的示例將按照Swift 1.1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.