Error calling another Viewcontroller per button [IOS 13]

Asked

Viewed 125 times

0

I’m studying about Clean-Swift and VIP architecture, and I’m having trouble doing the route part to call a Viewcontroller by the button. It may seem simple, but I don’t have much experience with Swift. I’m not using Storyboard, but Xib file If anyone can help me and explain where I might be missing I’d appreciate!

Homeviewcontroller

class HomeViewController: UIViewController
{

  var routerPassData = FistRouter()

  override func viewDidLoad()
 {
   super.viewDidLoad()
   self.view.backgroundColor = .blue
   let newBtn = UIButton()
       newBtn.setTitle("btn", for: .normal)
       newBtn.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Thin", 
       size: 50)
       newBtn.addTarget(routerPassData.self, action: #selector(routerPassData.teste), for: .touchUpInside)
       newBtn.frame =  CGRect(x: 15, y: 54, width: 300, height: 500)
       newBtn.setTitleColor(.black, for: .normal)
       self.view.addSubview(newBtn)
   }

 }

Homerouter

class HomeRouter: NSObject
{

 @objc func teste() {
 let pass = Bundle.main.loadNibNamed("secView", owner: self, options: 
 nil)?.first as? secViewController
 pass?.viewDidLoad()

}

When I put this test Func in the Viewcontroller I have the expected result which is to call the other Viewcontroller, only when I separate it from this error

this class is not key value coding-compliant for the key view.'

Where can I be missing?

1 answer

1

Hello I saw some errors in your code you are using the viewDidLoad to call another screen is not quite so see my code:

class HomeViewController: UIViewController {

    var newBtn: UIButton = {
        var btn = UIButton()
        btn.setTitle("btn", for: .normal)
        btn.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 50)
        btn.frame = CGRect(x: 15, y: 54, width: 300, height: 500)
        btn.addTarget(self, action: #selector(chamarOutraView), for: .touchUpInside)
        btn.setTitleColor(.black, for: .normal)
        return btn
    }()

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


    @objc func chamarOutraView(){
        // Primeiro você precisa registrar sua ViewController antes de chamar
        let newViewController = SegundaViewController(nibName: "SegundaViewController", bundle: nil)
        // Se desejar que a view fique em fullScreen adicione a linha a baixo
        newViewController.modalPresentationStyle = .fullScreen
        // E sempra para chamarmos outra view nao devemos usar o metodo viewDidLoad
        // E sim o present que é responsavel para chamar outra ViewController
        self.present(newViewController, animated: true, completion: nil)
    }
}

Then create your next Viewcontroller at first it doesn’t need to have anything:

class SegundaViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

I hope I helped. If you are starting a project now I tell you to see a little about how to do screen programmatically in my opinion is much better than NIB(XIB) and Storyboard.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.