简体   繁体   English

在uibutton.addTarget中动态分配“操作”

[英]Assign 'action' dynamically in uibutton.addTarget

please bear with me, as I'm new to swift -4 weeks old-. 请耐心等待,因为我刚开始学习-4周。

I've created the following 2 functions in fileA.swift 我在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

}

The reason behind the custombttn(theSelector:Selector) , is that i want to pass the function dynamically to the button in my viewcontroller file. custombttn(theSelector:Selector)背后的原因是,我想将函数动态传递给我的viewcontroller文件中的按钮。

Now, moving the fileB.swift, I have the following code... 现在,移动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!"
    }

}

Here is where things get tricky, when I attempt to build, I don't get any error (not even a warning). 在这里,事情变得棘手,当我尝试构建时,我没有收到任何错误(甚至没有警告)。 However, when I run the app, and tap the bttn in fileB.swift , I get the following error during runtime: 但是,当我运行该应用程序并点击bttn中的bttn时 ,在运行时出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton updatetxt]: unrecognized selector sent to instance 0x7f8453415670' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIButton updatetxt]:无法识别的选择器已发送到实例0x7f8453415670'

If I have 2 or more functions in my fileB.swift that I wish to assign dynamically to the action part of the addTarget, is there any way I can pass the selector dynamically to a button..? 如果我想在我的fileB.swift中有2个或更多函数要动态分配给addTarget的操作部分, 有什么方法可以将选择器动态传递给按钮。

Appreciate your time and assistance. 感谢您的时间和协助。 Please let me know if I need to explain something further. 如果需要进一步说明,请告诉我。

Yes, you can. 是的你可以。 The issue here is that you passed the button itself as the target for the action. 这里的问题是您将按钮本身传递为操作的目标。 Just pass the correct target when adding the action, which in this case is the instance of your view controller. 添加动作时只需传递正确的目标即可,在这种情况下,这就是您的视图控制器的实例。

It's crashing because your button target is wrong. 它崩溃是因为您的按钮目标错误。

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        
}

And use it like this 像这样使用

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!"
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-init()中的UIButton.addTarget崩溃 - Swift - Crash when UIButton.addTarget in init() 带有uibutton.addTarget到UIView的Swift类不起作用 - Swift class with uibutton.addTarget to UIView not working 带有Swift 3和本地功能的UIButton.addTarget - UIButton.addTarget with swift 3 and local function UIButton.addTarget 工作不正常。 似乎是一个时间问题? - UIButton.addTarget not working correctly. Seems to be a timing thing? 具有自定义视图的UIButton-addTarget:action:forControlEvents:不起作用 - UIButton with custom view - addTarget:action:forControlEvents: does not work UIButton的替代选项addTarget:action:forControlEvents:IOS中的方法 - Alternative option of UIButton addTarget:action:forControlEvents: method in IOS UIButton AddTarget多次在同一个目标操作上只调用一次? - UIButton AddTarget multiple times on same target action only calls once? SubClassing UIButton但我不能让“addTarget:action:forControlEvents:”执行? - SubClassing UIButton but I can't get “addTarget:action:forControlEvents:” to execute? addTarget:action:forControlEvents:无法识别的选择器发送到实例-uitablecell中的uibutton - addTarget:action:forControlEvents: unrecognized selector sent to instance - uibutton in uitablecell UIButton addTarget:action:forControlEvents:不能将自己用作目标吗? - UIButton addTarget:action:forControlEvents: not working using itself as a target?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM