ios Swift 以编程方式向按钮添加显示操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35550966/
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 08:34:02  来源:igfitidea点击:

Swift add show action to button programmatically

iosiphoneswift

提问by g_1_k

how can I add action to button programmatically. I need to add show action to buttons in mapView. thanks

如何以编程方式向按钮添加操作。我需要向 mapView 中的按钮添加显示操作。谢谢

let button = UIButton(type: UIButtonType.Custom) as UIButton

回答by Hari c

You can go for below code
`

你可以去下面的代码
`

let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn) 

for action

行动

 @objc func buttonAction(sender: UIButton!) {
        let btnsendtag: UIButton = sender
        if btnsendtag.tag == 1 {

            dismiss(animated: true, completion: nil)
        }
    }

回答by Muhammad Raheel Mateen

  let button = UIButton(type: UIButtonType.Custom) as UIButton
  button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)

  //then make a action method :

  func action(sender:UIButton!) {
     print("Button Clicked")
  }

回答by Rex

This works in Objective-C

这适用于Objective-C

You can create button like this

你可以像这样创建按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDragInside];
[button setTitle:@"Test Headline Text" forState:UIControlStateNormal];
button.frame = CGRectMake(20, 100, 100, 40);
[self.view addSubview:button];

Custom Action

自定义操作

 -(void)buttonAction {
       NSLog(@"Press Button");
 }

This works in latest Swift

这适用于最新的 Swift

Will you please create button like this

你能创建这样的按钮吗

let button = UIButton()
button.frame = CGRect(x: self.view.frame.size.width - 20, y: 20, width: 100, height: 100)
button.backgroundColor = UIColor.gray
button.setTitle("ButtonNameAreHere", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)

Custom Action

自定义操作

func buttonAction(sender: UIButton!) {
    print("Button tapped")
}

回答by Roberto Frontado

You need to add a Target to the button like Muhammad suggest

您需要像穆罕默德建议的那样向按钮添加一个目标

button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)

But also you need a method for that action

但你也需要一个方法来做那个动作

func action(sender: UIButton) {
    // Do whatever you need when the button is pressed
}

回答by Mukul Sharma

override func viewDidLoad() {
    super.viewDidLoad()  

    let btn = UIButton()
    btn.frame = CGRectMake(10, 10, 50, 50) 
    btn.setTitle("btn", forState: .Normal) 
    btn.setTitleColor(UIColor.redColor(), forState: .Normal) 
    btn.backgroundColor = UIColor.greenColor() 
    btn.tag = 1
    btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside)  //add button action
    self.view.addSubview(btn)
}

回答by Alexey

For Swift 4 use following:

对于 Swift 4,请使用以下内容:

button.addTarget(self, action: #selector(AwesomeController.coolFunc(_:)), for: .touchUpInside)

//later in your AswesomeController
@IBAction func coolFunc(_ sender:UIButton!) {
  // do cool stuff here
}