在 uibutton.addTarget 中动态分配 'action'
Assign 'action' dynamically in uibutton.addTarget
请耐心等待,因为我是 swift -4 周龄的新手 -.
我在 fileA.swift
中创建了以下 2 个函数
func custombttn(theSelector:Selector)-> UIButton{
let bttn = UIButton(frame: CGRect(x:20, y:400, width:200, height:30))
bttn.setTitle("tap this button", for: UIControlState.normal)
bttn.backgroundColor = UIColor.black
bttn.setTitleColor(UIColor.magenta, for: UIControlState.normal)
bttn.addTarget(bttn, action: theSelector, for: UIControlEvents.touchUpInside)
return bttn
}
func customtxtfld() -> UITextField{
let txtField = UITextField(frame: CGRect(x:20, y:360, width:200, height:30))
txtField.borderStyle = UITextBorderStyle.roundedRect
txtField.backgroundColor = UIColor.magenta
txtField.placeholder = "Do you like me now..?"
return txtField
}
custombttn(theSelector:Selector)
背后的原因是我想将该函数动态传递到我的 viewcontroller 文件中的按钮。
现在,移动 fileB.swift,我有以下代码...
class TabOneViewController: UIViewController{
let txt = customtxtfld()
let bttn = custombttn(theSelector: #selector(updatetxt))
override func loadView() {
super.loadView()
view.addSubview(txt)
view.addSubview(bttn)
}
func updatetxt(){
txt.text = "hello, you!"
}
}
这就是事情变得棘手的地方,当我尝试构建时,我没有收到任何错误(甚至没有警告)。但是,当我 运行 应用程序并在 fileB.swift 中点击 bttn
时,我在 运行 期间收到以下错误:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UIButton updatetxt]:
unrecognized selector sent to instance 0x7f8453415670'
如果我的 fileB.swift 中有 2 个或更多函数,我希望动态分配给 addTarget 的操作部分,有什么方法可以将选择器动态传递给按钮..?
感谢您抽出时间提供帮助。如果我需要进一步解释,请告诉我。
是的,你可以。这里的问题是您将按钮本身作为操作的目标传递。添加动作时只需传递正确的目标,在本例中是您的视图控制器的实例。
它正在崩溃,因为你的按钮目标是错误的。
func custombttn(target:Any, theSelector:Selector)-> UIButton{
let bttn = UIButton(frame: CGRect(x:20, y:400, width:200, height:30))
bttn.setTitle("tap this button", for: UIControlState.normal)
bttn.backgroundColor = UIColor.black
bttn.setTitleColor(UIColor.magenta, for: UIControlState.normal)
bttn.addTarget(target, action: theSelector, for: UIControlEvents.touchUpInside)
return bttn
}
然后像这样使用它
class TabOneViewController: UIViewController{
let txt = customtxtfld()
override func loadView() {
super.loadView()
view.addSubview(txt)
let bttn = custombttn(target:self, theSelector: #selector(updatetxt))
view.addSubview(bttn)
}
func updatetxt(){
txt.text = "hello, you!"
}
}
请耐心等待,因为我是 swift -4 周龄的新手 -.
我在 fileA.swift
中创建了以下 2 个函数func custombttn(theSelector:Selector)-> UIButton{
let bttn = UIButton(frame: CGRect(x:20, y:400, width:200, height:30))
bttn.setTitle("tap this button", for: UIControlState.normal)
bttn.backgroundColor = UIColor.black
bttn.setTitleColor(UIColor.magenta, for: UIControlState.normal)
bttn.addTarget(bttn, action: theSelector, for: UIControlEvents.touchUpInside)
return bttn
}
func customtxtfld() -> UITextField{
let txtField = UITextField(frame: CGRect(x:20, y:360, width:200, height:30))
txtField.borderStyle = UITextBorderStyle.roundedRect
txtField.backgroundColor = UIColor.magenta
txtField.placeholder = "Do you like me now..?"
return txtField
}
custombttn(theSelector:Selector)
背后的原因是我想将该函数动态传递到我的 viewcontroller 文件中的按钮。
现在,移动 fileB.swift,我有以下代码...
class TabOneViewController: UIViewController{
let txt = customtxtfld()
let bttn = custombttn(theSelector: #selector(updatetxt))
override func loadView() {
super.loadView()
view.addSubview(txt)
view.addSubview(bttn)
}
func updatetxt(){
txt.text = "hello, you!"
}
}
这就是事情变得棘手的地方,当我尝试构建时,我没有收到任何错误(甚至没有警告)。但是,当我 运行 应用程序并在 fileB.swift 中点击 bttn
时,我在 运行 期间收到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton updatetxt]: unrecognized selector sent to instance 0x7f8453415670'
如果我的 fileB.swift 中有 2 个或更多函数,我希望动态分配给 addTarget 的操作部分,有什么方法可以将选择器动态传递给按钮..?
感谢您抽出时间提供帮助。如果我需要进一步解释,请告诉我。
是的,你可以。这里的问题是您将按钮本身作为操作的目标传递。添加动作时只需传递正确的目标,在本例中是您的视图控制器的实例。
它正在崩溃,因为你的按钮目标是错误的。
func custombttn(target:Any, theSelector:Selector)-> UIButton{
let bttn = UIButton(frame: CGRect(x:20, y:400, width:200, height:30))
bttn.setTitle("tap this button", for: UIControlState.normal)
bttn.backgroundColor = UIColor.black
bttn.setTitleColor(UIColor.magenta, for: UIControlState.normal)
bttn.addTarget(target, action: theSelector, for: UIControlEvents.touchUpInside)
return bttn
}
然后像这样使用它
class TabOneViewController: UIViewController{
let txt = customtxtfld()
override func loadView() {
super.loadView()
view.addSubview(txt)
let bttn = custombttn(target:self, theSelector: #selector(updatetxt))
view.addSubview(bttn)
}
func updatetxt(){
txt.text = "hello, you!"
}
}