NSViewController do not dismiss

After update a project to Swift 4.2 I got this error message:


'dismissViewController' has been renamed to 'dismiss(_:)'


So I changed the code from:


dismissViewController(shareOptionsViewController)


to:


dismiss(shareOptionsViewController)


But the controller do not dismiss.


I am using Storyboards and using a segue of kind Sheet.


Before update to Swift 4.2, it works ok.


What is wrong in the above code?

Answered by Claude31 in 337111022

Why do you dismiss and not call:

dismissController(_ sender: Any?)


I also had a few points to clarify:

- Where is the segue declared ? In a button ?

- What is the initial view you segue from ?

- Where and when do you dismiss, from which window ?


Could you show more of your set up and code.


This could help:

https://stackoverflow.com/questions/32426294/how-to-create-modal-slide-out-window-in-mac-os/32427452

Accepted Answer

Why do you dismiss and not call:

dismissController(_ sender: Any?)


I also had a few points to clarify:

- Where is the segue declared ? In a button ?

- What is the initial view you segue from ?

- Where and when do you dismiss, from which window ?


Could you show more of your set up and code.


This could help:

https://stackoverflow.com/questions/32426294/how-to-create-modal-slide-out-window-in-mac-os/32427452

Hello Claude31, thanks for your answer.


In the link that you posted in one of the images there is a Received Actions in the Storyboard called dismissController:.


In my storyboard I did not find this action, I have the followings actions:


performClick:


print:


takeDoubleValueFrom:


takeFloatValueFrom:


takeIntValueFrom:


takeObjecttValueFrom:


takeStringValueFrom:


The segue is declared in the Viewcontoller , not in the button. ( I drag from Viewcontoller to ShareOptionsViewController)


The project has only the WindowController that comes with the Storyboard, for window content it has ViewController and there is other NSViewController called ShareOptionsViewController.


This is the code I am using:


I can see that the delegate methods are called, but the viewController do not dismiss.


import Cocoa
import CloudKit

class ViewController: NSViewController {

    var shareOptionsViewController : ShareOptionsViewController!
    
    enum SegueIdentifier : String {
        case segueToShareOptionsViewController
    }
    
    @IBAction func showShareOptionsViewController(_ sender: Any) {
          performSegue(withIdentifier:  SegueIdentifier.segueToShareOptionsViewController.rawValue, sender: self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        print("\(type(of: self)) \(#function)")
        guard let identifier =  segue.identifier else { fatalError("Can not get segue.identifier" ) }
        
        guard let segueIdentifier = SegueIdentifier(rawValue: identifier) else { fatalError("Can not get segueIdentifier") }
        
        switch segueIdentifier{

        case .segueToShareOptionsViewController:
            if let shareOptionsViewController = segue.destinationController as? ShareOptionsViewController{
                self.shareOptionsViewController = shareOptionsViewController
                self.shareOptionsViewController.delegate = self
                
                let frameSize = view.frame.size
                self.shareOptionsViewController.view.frame.size.width = frameSize.width / 2
                self.shareOptionsViewController.view.frame.size.height = frameSize.height / 2
                // self.shareOptionsViewController.view.animator().setFrameSize(frameSize)
            }
        }
    }

}

extension ViewController : ShareOptionsViewControllerDelegate{
    func dismissShareOptionsViewController(){
        print("\(type(of: self)) \(#function)")
          // dismissViewController(shareOptionsViewController) // 'dismissViewController' has been renamed to 'dismiss(_:)'
          // dismissController(shareOptionsViewController) // 'dismissController' has been renamed to 'dismiss(_:)'
         dismiss(shareOptionsViewController)
    }
    
    func share(sharingPermissionOptions options: NSSharingService.CloudKitOptions){
        print("\(type(of: self)) \(#function)")
        print("options.contains(.standard): \(options.contains(.standard))")
        print("options.contains(.allowPublic): \(options.contains(.allowPublic))")
        print("options.contains(.allowPrivate): \(options.contains(.allowPrivate))")
        print("options.contains(.allowReadOnly): \(options.contains(.allowReadOnly))")
        print("options.contains(.allowReadWrite): \(options.contains(.allowReadWrite))")
           
        
    // dismissViewController(shareOptionsViewController) // 'dismissViewController' has been renamed to 'dismiss(_:)'
       //dismissController(shareOptionsViewController) // 'dismissController' has been renamed to 'dismiss(_:)'
         dismiss(shareOptionsViewController)
    }
}
import Cocoa
import CloudKit

protocol ShareOptionsViewControllerDelegate : AnyObject {
    func share(sharingPermissionOptions: NSSharingService.CloudKitOptions)
    func dismissShareOptionsViewController()
}

class ShareOptionsViewController: NSViewController {
    weak var delegate : ShareOptionsViewControllerDelegate?
    
    @IBAction func sharePublicReadOnly(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPublic, NSSharingService.CloudKitOptions.allowReadOnly])
    }
    
    @IBAction func sharePublicReadWritte(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPublic, NSSharingService.CloudKitOptions.allowReadWrite])
    }
    
    @IBAction func sharePrivateReadOnly(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPrivate, NSSharingService.CloudKitOptions.allowReadOnly])
    }
    
    @IBAction func sharePrivateReadWritte(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPrivate, NSSharingService.CloudKitOptions.allowReadWrite])
    }
    
    @IBAction func cancel(_ sender: Any) {
        delegate?.dismissShareOptionsViewController()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(type(of: self)) \(#function)")
        view.wantsLayer = true   
        view.layer?.backgroundColor = NSColor.yellow.cgColor
    }
}

So:

- viewController is instanciated from the storyboard ?

- Is there a segue to transition to this viewController ?


Looks like you are not inspecting the same type of object as in the link

You were right, I was not inspecting the same type of object, I was inspecting the cancel buton instead of the ShareViewController.


I finally found a solution.


I was trying to dismiss ShareViewController from ViewController in the delegate methods of ShareOptionsViewControllerDelegate, and it was not working.


Now I place the code to dismiss ShareViewController in itself.


dismiss(sender)


And works ok.



import Cocoa
import CloudKit

protocol ShareOptionsViewControllerDelegate : AnyObject {
    func share(sharingPermissionOptions: NSSharingService.CloudKitOptions)
    func dismissShareOptionsViewController()
}

class ShareOptionsViewController: NSViewController {
    weak var delegate : ShareOptionsViewControllerDelegate?
    
    @IBAction func sharePublicReadOnly(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPublic, NSSharingService.CloudKitOptions.allowReadOnly])
        dismiss(sender)
    }
    
    @IBAction func sharePublicReadWritte(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPublic, NSSharingService.CloudKitOptions.allowReadWrite])
        dismiss(sender)
    }
    
    @IBAction func sharePrivateReadOnly(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPrivate, NSSharingService.CloudKitOptions.allowReadOnly])
        dismiss(sender)
    }
    
    @IBAction func sharePrivateReadWritte(_ sender: Any) {
        delegate?.share(sharingPermissionOptions:[NSSharingService.CloudKitOptions.allowPrivate, NSSharingService.CloudKitOptions.allowReadWrite])
        dismiss(sender)
    }
    
    @IBAction func cancel(_ sender: Any) {
        delegate?.dismissShareOptionsViewController()
        dismiss(sender)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(type(of: self)) \(#function)")
        view.wantsLayer = true   
        view.layer?.backgroundColor = NSColor.yellow.cgColor
    }
}

Thank you very much for your help.

Thank you for the feed back.


So, the proposed link did in fact provide the correct answer.

I marked your answer as correct, thanks.

NSViewController do not dismiss
 
 
Q