0

I'm writing an application which has an NSSplitViewController as the main View-Controller. I have it linked-up so that clicking a button in the menubar will trigger an @IBAction which then calls a function in one of the sub-View-Controllers.

if let board = storyboard {
    let imageController = board.instantiateController(withIdentifier: "VC_image_ID") as! VC_image
    imageController.viewDidAppear() // necessary or else I'll get an "Unexpectedly found nil" later on
    DispatchQueue.global().async{imageController.processImage(path)} // path variable was set earlier in the code, but not shown here
}

Inside the sub-View-Controller (named VC_image), I'm trying to change the stringValue of a label by using the following code:

public func processImage(_ path: String) {
    DispatchQueue.main.async {
        self.imageText.stringValue = path
        print(self.imageText.stringValue)
    }
}

Although the imageText.stringValue actually seems to have changed based on the fact that it prints the updated text (through the console), the text in the window never changes. Why is this happening? The reason is probably really obvious to professionals, but I'm still an amateur and can't figure it out. Thanks.

htmlcat
  • 336
  • 3
  • 10
  • 1
    Well, for one thing, _calling_ `viewDidAppear` is totally illegal. It's an _event_, a signal that comes from Cocoa; for _you_ to call it utterly wrong. So I would suggest you start by worrying about why you think you need to do _that_. – matt Jun 18 '20 at 22:45
  • I explained it in the comments on the code. I think it has the error because `viewDidAppear` somehow initiates it so that the text can be changed. How do you suggest doing it? – htmlcat Jun 19 '20 at 03:37
  • 1
    You didn’t “explain” it at all. What’s nil? You make a view controller but you do nothing with it? It all makes no sense. You’re concealing everything of importance. – matt Jun 19 '20 at 03:51
  • Sorry for being unclear. Initially I was trying to call the function directly using `VC_image().processImage(path)`, but the problem is that the section which changes the `stringValue` of the label would have an error which said "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value." For some reason, the code was acting like the `@IBAction` didn't exist. So I researched some and found a [website](https://www.meandmark.com/blog/2017/05/instantiating-views-from-mac-storyboards/) which suggested doing it w/ the `imageController` method. – htmlcat Jun 19 '20 at 04:13
  • ...That method actually worked, except for the issue w/ the text not being updated. The view controller doesn't do "nothing," I just didn't feel that I needed to include code which doesn't have to do w/ my problem. – htmlcat Jun 19 '20 at 04:21
  • Please post the original issue without the fixes. It looks like you're calling the action on a new view controller instead of the existing view controller. – Willeke Jun 19 '20 at 09:15
  • The original issue was [this](https://stackoverflow.com/questions/62333844/how-do-i-set-the-text-of-a-label-from-outside-of-viewdidappear?noredirect=1) question, but I was figuring it out as I went so I thought I should post a new question w/ my current problem. – htmlcat Jun 19 '20 at 18:55

0 Answers0