Zulqarnain Naveed
Zulqarnain Naveed

Reputation: 108

How to move from one NS View controller to another while closing the previous view controller in OS X swift?

I am on NSView Controller B where I came from A using

**let vc = RegisterViewController.registerViewController()
vc.title = "Sign up"
self.presentViewControllerAsModalWindow(vc)**

Now from B, I want to go to C while closing A as my B is opened in separate window.

Upvotes: 1

Views: 92

Answers (2)

Zulqarnain Naveed
Zulqarnain Naveed

Reputation: 108

Another Thing that worked for was this solution.

if let window = NSApplication.shared.mainWindow {
        let VC = ViewController.VC()
        window.contentViewController = VC
        if let windowController = window.windowController as? homeWindow {
            
        }
    }
 self.view.window?.close()

Where the function VC() is.

class func VC() -> ViewController {
    return NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "VC")) as! ViewController
}

Upvotes: 0

M Afham
M Afham

Reputation: 960

I have wrote an extension for this, you can close any view controller using it:

extension NSApplication{
  static func closeWindow(withVCType vcType: NSViewController.Type)
  {
    // Find the window of desired view controller by comparing class name
    if let windowOfA = shared.windows.first(where: {
        $0.contentViewController?.className == vcType.className()
    }){
        // Close it
        windowOfA.close()
    }
 } 

}

So in order to close let say view controller A, call it like this:

NSApplication.closeWindow(withVCType: A.self)

Upvotes: 1

Related Questions