Hello
I created a class NSObject, in this class I create a button, when the user touch this button I need to show a ViewController, but I have the error message:
Value of type 'CarregarMenu' has no member 'navigationController'
On this line
self.navigationController!.pushViewController(vc, animated: true)
This is the code:
import Foundation
import UIKit
class CarregarMenu: NSObject {
......
@objc func btnPeidosFinalizadosAction(sender: UIButton!){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "PedidosFinalizadosController")
self.navigationController!.pushViewController(vc, animated: true) // this line shows error
}
......
override init() {
super.init()
}
}How can I fix it? This class is generic and can be called on everywhere on my app
1. Pass a reference of a UIViewController this is encapsulated by a UINavigationContoller as its rootViewController.
---OR---
2. If #1 above is too complex then implement a delegate protocol pattern that calls back out to the UIViewController subclass, which is encapsulated by a UINavigationController as it rootViewController to invoke the push.
---OR---
3. Just move the code out of the NSObject subclass into the UIViewController subclass
Keep all UI logic in the UIViewController layer of your code MVC or google how to implement MVVM design patterns correctly.