-1

I have a function in my TuesdayViewController.swift that creates a button.

Whenever it's Tuesday, I want that button to appear in my ViewController.swift

I have this function in my TuesdayViewController:

func tuesdayView(){
    let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
    button.backgroundColor = .red
    button.setTitle("Test button", for: .normal)
    self.view.addSubview(button)

}

If I call this function in Tuesday's viewDidLoad(), the button shows in the TuesdayViewController.

How can I make it so I call this function through my main ViewController and for the button to show in the main ViewController screen?

I have tried this in my main ViewController but the button doesn't appear:

override func viewDidLoad(){

super.viewDidLoad()
TuesdayViewController().tuesdayVew()


}

Any help would be appreciated!

2 Answers2

0

You need to use an observer with NotificationCenter:

First you need to create a Notification Name

extension Notification.Name {
    static let notName = Notification.Name("notName")
}

TuesdayViewController:

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(tuesdayViewObserver), name: .notName, object: nil)

}

@objc func tuesdayViewObserver(_ notification: Notification) {
    //Here you call the tuesdayView function
    tuesdayView()
}

And in the controller in which you want to fire the method, you post the notification and the method will be executed:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.post(name: .notName, object: nil)
}

For further info, I recommend you to take a look to NotificationCenter and how it works. LINK

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Karlo A. López
  • 2,548
  • 3
  • 29
  • 56
  • `@objc func tuesdayViewObserver(_ notification: Notification)` you should drop the NS prefix and if coding in Swift 4 you need to add @objc in front of your method. Also in your selector (notification:) it is not needed it can be inferred by the compiler `#selector(tuesdayViewObserver)` https://stackoverflow.com/a/30541063/2303865 – Leo Dabus Oct 19 '17 at 03:34
0

If I understand your question right, you just want to add the same kind of button from your TuesdayViewController into your MainViewController, and not adding the button to the TuesdayViewController by calling it in the MainViewController.

To do that, you just can declare your tuesdayView as a public property of your TuesdayViewController, like so:

TuesdayViewController.swift

class TuesdayViewController: UIViewController {
    public lazy var tuesdayView: UIButton = {
        let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
        button.backgroundColor = .red
        button.setTitle("Test button", for: .normal)
        return button
    }()

    override func viewDidLoad(){
        super.viewDidLoad()
        self.view.addSubview(self.tuesdayView)
    }
}

And call that property like what you've been doing before.

MainViewController.swift

class MainViewController: UIViewController {
    override func viewDidLoad(){
        super.viewDidLoad()
        let button = TuesdayViewController().tuesdayView
        self.view.addSubview(button)
    }
}
Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95