Problem to user navigationController

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

Accepted Reply

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.

Replies

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.

>> in this class I create a button


No, you don't, and the concept of creating a button "in" a class makes no sense.


So, there is some question about how you connection your button's action to the "btnPeidosFinalizadosAction" method of an instance of this class — and where the instance of this class is created, and who keeps a reference to it.


In general, you create a button in a view, which may have a view controller, which may have an action method that the button uses. A UIViewController has a "navigationController" property (which refers to the parent nav controller if it has one), but your CarregarMenu isn't a UIViewController, so the property doesn't exist.


>> This class is generic …


No, it's not. "Generic" means something special in Swift. Presumably you mean this class is general-purpose.


>>…and can be called on everywhere on my app


Well, no, that's not right either. Your "btnPeidosFinalizadosAction" is an instance method, so you need to "call on" an instance of the class, not the class itself. That means it's important to clarify whether there's one instance, or multiple instances, and make sure you're using the correct instance.


>> @objc func btnPeidosFinalizadosAction(sender: UIButton!){


If this function is intended to be an action method, it should be marked @IBAction (which implies @objc).

Alex:


>This class is generic


We know what you mean, but about that: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html


Ken