ios 未按下按钮时如何调用按钮功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29986738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:51:05  来源:igfitidea点击:

How do I call a button function when the button is not being pressed

iosswiftswift2ibaction

提问by nachshon fertel

I have an IBAction connected to a button, and I wanted to know if there is any way to run that function even if the button is not being pressed. This is what I tried...

我有一个连接到按钮的 IBAction,我想知道即使没有按下按钮,是否有任何方法可以运行该功能。这是我试过的...

Note: I am using swift for this project.

注意:我在这个项目中使用 swift。

//I get an error when I type this code?
self.buttonPressed()

@IBAction func buttonPressed(sender: AnyObject) {

    print("Called Action")

}

回答by Satachito

Make your sender argument optional and pass nil to ButtonPressed.

使您的发件人参数可选并将 nil 传递给 ButtonPressed。

self.ButtonPressed( nil )


@IBAction func ButtonPressed( sender: AnyObject? ) {
    println("Called Action")
}

回答by Chackle

One way would be to link your button up to its respective interface builder button and pass it into your function when you call it.

一种方法是将您的按钮链接到其各自的界面构建器按钮,并在您调用它时将其传递到您的函数中。

@IBOutlet weak var yourButton: UIButton!

self.buttonPressed(yourButton)

@IBAction func buttonPressed(sender: AnyObject) {
    print("Called Action")
}

Alternatively, define your function like so, then you'll be able to call your method the same way as you did before:

或者,像这样定义你的函数,然后你就可以像以前一样调用你的方法:

@IBAction func buttonPressed(sender: AnyObject? = nil) {
    print("Called Action")
}

// Call your method like this
self.buttonPressed()

// or this
self.buttonPressed(sender: nil)

回答by Matthew Quiros

Your ButtonPressedfunction needs an argument senderand yet you're not passing any when you call it. However, if you're doing this programatically, then you obviously don't have a sender.

您的ButtonPressed函数需要一个参数sender,但您在调用它时没有传递任何参数。但是,如果您以编程方式执行此操作,那么您显然没有sender.

Two ways to get around this:

解决这个问题的两种方法:

  • Make the sender parameter an optional (AnyObject?instead of AnyObject) and invoke self.ButtonPress(nil). I just tried this and it works.
  • Put all the button press function in a separate function, e.g., performButtonPress(). Call it from inside the IBAction outlet, and if you want to press the button programatically, directly call performButtonPress().
  • 将 sender 参数设为可选(AnyObject?而不是AnyObject)并调用self.ButtonPress(nil). 我刚试过这个,它的工作原理。
  • 将所有按钮按下功能放在一个单独的功能中,例如,performButtonPress()。从 IBAction outlet 内部调用它,如果你想以编程方式按下按钮,直接调用performButtonPress().

回答by Ram Madhavan

Call a button action using the following format: Swift 3

使用以下格式调用按钮操作:Swift 3

@IBAction func buttonSelected(_ sender: UIButton) {
        print("Butten clicked")
}

To call the button action:

调用按钮动作:

let button = UIButton()
self.buttonSelected(button)

回答by Urvish Modi

    lazy var Button : UIBarButtonItem = {
      let barButtonItem = UIBarButtonItem(title: "Button", style: .done, target: self, action: #selector(btnSelection))
      return barButtonItem
    }()


    func btnSelection(){
     // Code
    }

    // Now you can call self.btnSelection() whenever you want

回答by ooenomel2003

You need to use "self" as "sender", example:

您需要使用“self”作为“sender”,例如:

self.buttonPressed (sender: self)

or a simpler version

或者更简单的版本

buttonPressed (sender: self)

Both tested in Swift 3

两者都在 Swift 3 中测试过