Niloy Mahmud
Niloy Mahmud

Reputation: 97

Navigate to a View Controller from a class which is not a View Controller

I have a class name "DatabaseHandler.swift",but it is not a UIViewController.I want to go to a View Controller from my "DatabaseHandler.swift"

How do I do that?

EDIT: My DatabaseHandler is intialized like this-

    class DatabaseHandler: NSObject {
    let sharedInstance = DatabaseHandler()
    var database: FMDatabase? = nil
    class var instance:DatabaseHandler{
        sharedInstance.database = FMDatabase(path: Utils.getPath("DataBaseDemo.sqlite"))
        var path = Utils.getPath("DataBaseDemo.sqlite")
        println("path : \(path)")

        sharedInstance.database!.open()
.
.
.
.
.
.


    }
    }

Upvotes: 0

Views: 105

Answers (1)

nanothread
nanothread

Reputation: 908

You could make a protocol in which you could tell the current view controller to open the next one. Write this before class DatabaseHandler:

protocol DatabaseHandlerDelegate{
    func shouldSwitchToNewView()
}

Then in the class declare a variable:

var delegate: DatabaseHandlerDelegate?

And when you want to switch views go:

delegate?.shouldSwitchToNewView()

Then, in your view controller, conform to the protocol in the class line:

class MyVC: UIViewController, DatabaseHandlerDelegate{}

And implement the function in your VC:

func shouldSwitchToNewView(){
// Switch here!
}

Then, all that's left to do is set you as the delegate in viewDidLoad() like this:

let handler = DatabaseHandler()

override func viewDidLoad(animated: a){
    super.viewDidLoad(animated: a)
    handler.delegate = self
}

Upvotes: 1

Related Questions