0

I've only been coding for a few weeks so forgive my no-doubt clumsy code.

I have a NSTextField that needs to update based upon a selection from an NSPopupButton

These are my outlets:

@IBOutlet weak var inputText: NSTextField!
@IBOutlet weak var outputText: NSTextField!
@IBOutlet weak var inputBrowseButton: NSButton!
@IBOutlet weak var outputBrowseButton: NSButton!
@IBOutlet weak var codecSelector: NSPopUpButton!

These are my variables:

// derive default path and filename from @IBAction of inputBrowseButton
var defaultUrl: URL! 
// derived from default of inputBrowse unless outputBrowse is used
var outputUrl: URL!
// change output path from @IBAction of outputBrowseButton
var outputDirectory: URL! 
// change path extension from @IBAction of codecSelector NSPopupButton
var codecChosen: String = "" 
// dictionary of arguments to form script for ffmpeg
var ffmpegCodecArg: String = ""

And here is my method to pull all that together:

// construct output path for use in ffmpegArguments dictionary and display in outputText NSTextField
func updateOutputText(defaultUrl: URL?, outputDirectory: URL?) -> String {
    if defaultUrl != nil && outputDirectory != nil {
        let outputFile = defaultUrl!.deletingPathExtension()
        let outputFilename = outputFile.lastPathComponent
        outputUrl = outputDirectory!.appendingPathComponent(outputFilename)
        outputText.stringValue = outputUrl.path + "\(codecChosen)"
} else if defaultUrl == nil && outputDirectory != nil {
        outputText.stringValue = outputDirectory!.path
} else {
        outputUrl = defaultUrl!.deletingPathExtension()
        outputText.stringValue = outputUrl.path + "\(codecChosen)"
}
    return outputText.stringValue
}

Now at the present this function isn't working because I haven't figured out how to call it yet. But that's an issue for another time, not what I'm asking about here.

Previously I was running

        outputUrl = defaultUrl!.deletingPathExtension()
        outputText.stringValue = outputUrl.path + "\(codecChosen)"

as part of my inputBrowseButton @IBAction method, and I was running

        let outputFile = defaultUrl!.deletingPathExtension()
        let outputFilename = outputFile.lastPathComponent
        outputUrl = outputDirectory!.appendingPathComponent(outputFilename)
        outputText.stringValue = outputUrl.path + "\(codecChosen)"

as part of my outputBrowseButton @IBAction method.

Which worked fine, EXCEPT when it came to updating outputText.stringValue when the codecChosen variable was assigned a new value in my @IBAction for the codecSelector NSPopupButton method.

I suspect the problem is that I don't have my outputText NSTextField set up to update when the codecSelector NSPopupButton changes. What do I need to do to make that happen?

AlexH
  • 828
  • 7
  • 26
NCrusher
  • 93
  • 1
  • 10

1 Answers1

0

How do you set the text in an NSTextField?

Which worked fine, EXCEPT when it came to updating outputText.stringValue when the codecChosen variable was assigned a new value in my @IBAction for the codecSelector NSPopupButton method.

Try using a separate method to change the text.

func changeText(inputString: String, inputField: NSTextField) {
    inputField.stringValue = inputString 
}

Separating your code more might make it easier for you to keep track of while you are starting out. I am not entirely familiar with macOS, so that method may not work, but try working with the principle of it and see if you can progress.

ekrenzin
  • 387
  • 2
  • 12
  • 1
    Hi and thank you. If I understand what you're saying, I think that's what I was trying to do with the function I included, beginning with `func updateOutputText(defaultUrl: URL?, outputDirectory: URL?) -> String` which I will be working on to get to work. But my question was about getting the NXTextField to update when the value of one of the variables required to build the string (`codecChosen`) changes based on input from my NSPopupButton. Are you saying that should happen automatically if I get my separate function working properly? – NCrusher Oct 15 '19 at 23:49
  • I think I get what you were saying now. I called the function to update the outputText field in my IBaction method for the codecSelector popup and it's now working. I just need to figure out the issue of having a default setting for the popup now. – NCrusher Oct 16 '19 at 06:26
  • ... which I did in a very similar way. I created a method for the codecSelector popup and called it in my methods to choose the input and output files. Now everything is working. – NCrusher Oct 16 '19 at 07:07
  • I'm glad everything is working for you now! If I helped at all would you mind giving me an upvote? – ekrenzin Oct 16 '19 at 13:03
  • I did, though it doesn't affect the visible score because I'm new too. – NCrusher Oct 16 '19 at 19:23
  • haha no problem! I'm just glad I was able to help out a bit, especially as you are working on a mac app. Out of curiosity have you tried working with the new cross platform features? It's incredible seeing my ipad apps running on my imac – ekrenzin Oct 16 '19 at 19:47
  • Not yet. This is literally my first attempt at making an app. I think I may try the Catalyst thing with my next project, which would be ideally suited to run on either iPad or Mac. After that, I may try transferring the projects to RemObject's Silver so that I can try to make them compatible with PC/Android. – NCrusher Oct 17 '19 at 00:06