ios Swift3:使用代码添加按钮

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

Swift3: Add button with code

iosswift2swift3

提问by pRivaT3 BuG

Im reading Apples swift (iOS) documentation but its written for Swift 2 and i use Swift 3. I want to add a button programmatically but its seems there is a change and I can't find how to fix it.

我正在阅读 Apples swift (iOS) 文档,但它是为 Swift 2 编写的,我使用的是 Swift 3。我想以编程方式添加一个按钮,但它似乎发生了变化,我找不到如何修复它。

Here is the Code for the Swift 2 example:

这是 Swift 2 示例的代码:

import UIKit

class RatingControl: UIView {

// MARK: Initialization

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Buttons
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
    button.backgroundColor = UIColor.red()
    button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
    addSubview(button)
}

override func intrinsicContentSize() -> CGSize {
    return CGSize(width: 240, height: 44)
}

// MARK: Button Action

func ratingButtonTapped(button: UIButton){
    print("Button pressed")
}
}

The only change i made after the 'fix-it' showed the error is this in the selector:

在“修复它”显示错误后我所做的唯一更改是选择器中的这个:

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchDown)

This should have printed "Button pressed" but it doesn't. Any help?

这应该打印“按下按钮”,但它没有。有什么帮助吗?

回答by V? ?ình Thu?n

My code:

我的代码:

button.backgroundColor = UIColor.red

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), for: .touchDown)

override var intrinsicContentSize : CGSize {
//override func intrinsicContentSize() -> CGSize {
    //...
    return CGSize(width: 240, height: 44)
}

// MARK: Button Action
func ratingButtonTapped(_ button: UIButton) {
    print("Button pressed ")
}

回答by axel

Try something like this. I haven't tested but it should work:

尝试这样的事情。我还没有测试过,但它应该可以工作:

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(ratingButtonTapped), for: .touchUpInside)
addSubview(button)

func ratingButtonTapped() {
    print("Button pressed")
}

回答by pRivaT3 BuG

Found the solution. For some reason the:

找到了解决办法。出于某种原因:

func ratingButtonTapped(button: UIButton)

needs an "_" before button. So it should be :

按钮前需要一个“_”。所以应该是:

func ratingButtonTapped(_ button: UIButton)

And the other part of the code must be :

代码的另一部分必须是:

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), for: .touchDown)

Thanks for helping :) Your method may be correct also but thats the one Apple wants it.

感谢您的帮助 :) 您的方法也可能是正确的,但这就是 Apple 想要的方法。