【问题标题】:Buttons Programmatically on Swift 4Swift 4 上的按钮编程
【发布时间】:2018-08-02 01:13:16
【问题描述】:

一个简单的问题,我正在尝试创建一个名为 setupHeader() 的函数,在该函数中调用时,会设置标题的所有视图和按钮。问题出在“注销”按钮上,由于某种原因我无法正确操作。

第一个错误是按钮的addTarget不能识别“self”。有什么建议吗?

第二个是#selector 只适用于@objc func,该函数不能在主setupHeader 函数中。我应该把它放在哪里?这是我的代码的 sn-p:

import Foundation
import UIKit

func setupHeader(vc: UIViewController) {

let headerFiller: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = supaGray
    return view
}()

let header: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = supaGray
    return view
}()

let logOutButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Log Out", for: .normal)
    button.setTitleColor(.blue, for: .normal)
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.textAlignment = .left
    button.addTarget(self, action: #selector(handleLogOutButton), for: .touchUpInside)
    return button
}()

func handleLogOutButton() {

}

vc.view.addSubview(header)
header.topAnchor.constraint(equalTo: vc.view.safeAreaLayoutGuide.topAnchor).isActive = true
header.leftAnchor.constraint(equalTo: vc.view.leftAnchor).isActive = true
header.rightAnchor.constraint(equalTo: vc.view.rightAnchor).isActive = true
header.heightAnchor.constraint(equalToConstant: 40).isActive = true

header.addSubview(logOutButton)
logOutButton.centerYAnchor.constraint(equalTo: header.centerYAnchor, constant: -5).isActive = true
logOutButton.leftAnchor.constraint(equalTo: header.leftAnchor, constant: 7).isActive = true
logOutButton.widthAnchor.constraint(equalTo: header.widthAnchor, multiplier: 0.17).isActive = true
logOutButton.heightAnchor.constraint(equalToConstant: 40).isActive = true

vc.view.addSubview(headerFiller)
headerFiller.topAnchor.constraint(equalTo: vc.view.topAnchor).isActive = true
headerFiller.leftAnchor.constraint(equalTo: vc.view.leftAnchor).isActive = true
headerFiller.rightAnchor.constraint(equalTo: vc.view.rightAnchor).isActive = true
headerFiller.bottomAnchor.constraint(equalTo: header.topAnchor).isActive = true
}

【问题讨论】:

    标签: swift button swift4 programmatically


    【解决方案1】:

    以这种方式声明您的按钮按下回调。

    @objc func handleLogOutButton() {
    
    }
    

    对于自我错误,请使用 vc 作为函数中的上述参数。

    【讨论】:

    • 感谢您的快速回复!
    【解决方案2】:

    看起来您正在将视图控制器传递给您的函数,因此使用 self 将不起作用。尝试使用“vc”

    【讨论】:

      【解决方案3】:

      非常简单:

      如下所示修改你的代码:

      lazy var logOutButton: UIButton = {
      let button = UIButton()
      button.translatesAutoresizingMaskIntoConstraints = false
      button.setTitle("Log Out", for: .normal)
      button.setTitleColor(.blue, for: .normal)
      button.titleLabel?.adjustsFontSizeToFitWidth = true
      button.titleLabel?.textAlignment = .left
      button.addTarget(self, action: #selector(handleLogOutButton), for: .touchUpInside)
      return button
      

      }()

      @objc func handleLogOutButton() {
      // Do your things here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-23
        • 1970-01-01
        相关资源
        最近更新 更多