简体   繁体   中英

Swift: Calling UIButton press from another class

I have my ViewController.class and a 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.

This is how my ViewController looks like:

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?

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.

If you subclass any UIView you should setup the frames in 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

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")
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM