skip to Main Content

I have multiple container views on MacOS (Xcode/AppKit) which all embed segue to the same PistonViewController class, as in below image.
enter image description here

Each container view has an identifier, e.g. "multiPiston1, multiPiston2", etc…
I would like the PistonViewController has an array of buttons with IBActions. On button action, I would like to get the identifier of the correct Container View, so that I know in which view the action is taking place.

I have tried the following in the PistonViewController class:

class pistonViewController: NSViewController {
...
    @IBAction func pistonClick(_ sender: NSButton) {
    if self.view.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1")
    
    if self.parent.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1")
    
    if self.parent.view.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1") 
} }

All of them, upon printing, give the wrong identifiers. So how do I find this identifier?

EDIT: Alternatively, each embed segue is separate – if I could somehow get the identifier of the embed segue, then I could also find out which Container View is being used.

EDIT: (my understanding of) web of relationships.

View Controllers: MainController -> PistonViewController

NSViews: MainControllerView -> ContainerView -> PistonView (view of PistonViewController)

2

Answers


  1. Chosen as BEST ANSWER

    NSViewControllers and NSViews live in separate domains, so to speak. So since the ContainerView is not a ViewController, to solve this problem it was necessary to communicate via NSViews.

    Subclass the ContainerView and set identification attributes in the subview, i.e. the NSView of the PistonViewController:

    class ContainerView1: NSView {
    
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
    
            // Drawing code here.
        }
        override func viewDidMoveToWindow() {
             //print("hello???")
            print(self.subviews)
            let rail = self.subviews[0] as! pistonRail
            rail.container_id = self.identifier?.rawValue
        }
    }
    

    The view subclass of the PistonViewController:

    class pistonRail: NSView {
        var container_id: String?
       
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
    
            // Drawing code here.
        }
    }
    

    Access the NSView attributes of the PistonViewController through self.view:

    class pistonViewController: NSViewController {
        @IBAction func pistonClick(_ sender: NSButton) {
                
                let pistonRail = self.view as! pistonRail
                print("view id: " + (pistonRail.container_id ?? "no"))
            }
    }
    

  2. You can set Restoration ID for each Container on Storyboard and use like this

    class ContainerViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        print(self.restorationIdentifier)
        
    }
    

    }

    Set Restoration ID here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search