斯威夫特:从另一个班级打电话给UIButton

时间:2023-01-07 22:56:58

I have my ViewController.class and a Menu.class

我有我的ViewController.class和一个Menu.class

In the Menu.class I create and setup all the buttons and in the ViewController.class I add the Menu to the view. When I run the code everything is shown on the screen but I am not able to press the button.

在Menu.class中,我创建并设置了所有按钮,在ViewController.class中,我将菜单添加到视图中。当我运行代码时,屏幕上会显示所有内容,但我无法按下按钮。

This is how my ViewController looks like:

这就是我的ViewController的样子:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let menu = Menu()
        menu.setupView(controller: self, width: 600, height: 120)
        self.view.addSubview(menu)

        // Do any additional setup after loading the view, typically from a nib.
    }
}

And this is my Menu:

这是我的菜单:

import Foundation
import UIKit

class Menu: UIView{

    func setupView(controller: ViewController, width: CGFloat, height: CGFloat){

        let newGame = UIButton(frame: CGRect(x: controller.view.center.x, y: 300, width: width, height: height))
        newGame.center = CGPoint(x: controller.view.center.x, y: 300)
        newGame.setTitle("New Game", for: .normal)
        newGame.backgroundColor = UIColor.gray
        newGame.titleLabel?.font = UIFont(name: "Helvetica", size: 42)
        newGame.setTitleColor(UIColor.black, for: .normal)
        newGame.addTarget(self, action: #selector(Menu.newGame), for: .touchUpInside)
        self.addSubview(newGame)

    }
    func newGame(){
        print("New Game")

    }
}

What is my mistake. Do I need to do more initializing so it is able to detect a press?

我的错是什么我是否需要进行更多初始化以便能够检测出印刷机?

1 个解决方案

#1


2  

This is how you should do it. Repeat the operation for buttonTwo and label. Setup the view in viewDidLoad() and setup the frames in viewDidLayoutSubviews.

这是你应该怎么做的。重复buttonTwo和label的操作。在viewDidLoad()中设置视图并在viewDidLayoutSubviews中设置框架。

If you subclass any UIView you should setup the frames in layoutSubviews()

如果您将任何UIView子类化,则应在layoutSubviews()中设置框架

If you want to display a tableView when you click on newGame then create a new UIViewController. Add a UITableView in it, the same way you did add Button and Label in ViewController

如果要在单击newGame时显示tableView,请创建一个新的UIViewController。在其中添加UITableView,就像在ViewController中添加Button和Label一样

import UIKit

class ViewController: UIViewController {

  let buttonOne = UIButton()
  let buttonTwo = UIButton()
  let label = UILabel()

  override func viewDidLoad() {
    super.viewDidLoad()

    buttonOne.backgroundColor = UIColor.gray
    buttonOne.setTitle("New Game", for: .normal)
    buttonOne.titleLabel?.font = UIFont(name: "Helvetica", size: 42)
    buttonOne.setTitleColor(UIColor.black, for: .normal)
    buttonOne.addTarget(self, action: #selector(newGame), for: .touchUpInside)

    view.addSubview(buttonOne)
    view.addSubview(buttonTwo)
    view.addSubview(label)
  }

  override func viewDidLayoutSubviews() {
    buttonOne.frame = CGRect(x: 0, y: 300, width: 600, height: 120)
    buttonOne.center.x = self.view.center.x
  }

  func newGame(sender: UIButton?) {
    print("New Game")
  }
}

#1


2  

This is how you should do it. Repeat the operation for buttonTwo and label. Setup the view in viewDidLoad() and setup the frames in viewDidLayoutSubviews.

这是你应该怎么做的。重复buttonTwo和label的操作。在viewDidLoad()中设置视图并在viewDidLayoutSubviews中设置框架。

If you subclass any UIView you should setup the frames in layoutSubviews()

如果您将任何UIView子类化,则应在layoutSubviews()中设置框架

If you want to display a tableView when you click on newGame then create a new UIViewController. Add a UITableView in it, the same way you did add Button and Label in ViewController

如果要在单击newGame时显示tableView,请创建一个新的UIViewController。在其中添加UITableView,就像在ViewController中添加Button和Label一样

import UIKit

class ViewController: UIViewController {

  let buttonOne = UIButton()
  let buttonTwo = UIButton()
  let label = UILabel()

  override func viewDidLoad() {
    super.viewDidLoad()

    buttonOne.backgroundColor = UIColor.gray
    buttonOne.setTitle("New Game", for: .normal)
    buttonOne.titleLabel?.font = UIFont(name: "Helvetica", size: 42)
    buttonOne.setTitleColor(UIColor.black, for: .normal)
    buttonOne.addTarget(self, action: #selector(newGame), for: .touchUpInside)

    view.addSubview(buttonOne)
    view.addSubview(buttonTwo)
    view.addSubview(label)
  }

  override func viewDidLayoutSubviews() {
    buttonOne.frame = CGRect(x: 0, y: 300, width: 600, height: 120)
    buttonOne.center.x = self.view.center.x
  }

  func newGame(sender: UIButton?) {
    print("New Game")
  }
}