Post

Replies

Boosts

Views

Activity

Reply to @published var not updating view
Ah I figured it out. Man, I always do this. I declare two @StateObjects of the same view model. I forget that you can only declare ONE @StateObject view model, and then any time you need to reference it you must use @ObservedObject var myViewModel: MyViewModel. And notice, not MyViewModel(). And then you have to try to figure out how to make the preview happy, which is not easy.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’22
Reply to Open Recent Menu only works inside Xcode.
In case anyone else is struggling with getting "open recent" to work in a non-document based app... this may help. You have likely written your own 'open file' function, probably in your ViewController... somewhere in there drop this line: NSDocumentController.shared.noteNewRecentDocumentURL(myFileURL) where myFileURL is an unwrapped URL (not NSURL, and definitely not a string). This line instantiates the document controller and should get your URL (now magically shortened to just the file name!) to the open recent menu. You do not need to instantiate the document controller elsewhere, this line does that. Now that the menu is being fed URLs from your app, you need to create the logic to open a file when the user selects a file to open. In your AppDelegate, create this function: func application(_ sender: NSApplication, openFile filename: String) -> Bool {     let url = URL(fileURLWithPath: filename)           if let controller = NSApplication.shared.mainWindow?.contentViewController as? ViewController {       let result = controller.openFunction(url: url)       return result     } else {       print("could not instantiate controller object in AppDelegate.")       return false     }   } Notice: the URL is being returned as a string. The next line turns it back into a URL, and even handles white space, which the usual let url = URL(string: filename) does not. In the next line I will instantiate a handle to my ViewController class, if your file opening logic is elsewhere, instantiate accordingly. The next line calls my 'openFunction' function in my viewController, which takes a URL and returns a bool related to the success of opening the file. This is now functional in my app, hopefully it will work in your own.
Topic: UI Frameworks SubTopic: AppKit Tags:
Feb ’21
Reply to how to force-exit editing from a textField?
Ah I figured it out. I had subclassed AVPlayerView because I didn't like the playback controls, was using keyDown to capture events. However, when you click in a textField, Field Editor takes over and overrides the keyboard with its own behaviors, so anything that you assign via keyDown does nothing. I'd still like to find a way to navigate how to trap that Field Editor key data and re-route it... but so little information on how you would do that. There is some information in the Cocoa Text Architecture Guide on "Intercepting Key Events," ... looks maybe like doCommand or doCommandBySelector but zero context on what to call those on, where they would go, etc. Went with capturing a mouseDown event, that's working for me! Cheers...
Topic: App & System Services SubTopic: Core OS Tags:
Feb ’21
Reply to need help understanding how Document class fits into MVVM
Well, after wrangling with this Document file for a couple days, I've come to the conclusion that the best approach is to toss the thing out, and just write my own code for open, save, save as... etc. functions. Plus 1 for Apple trying to make things easier, but instead making them damn near impossible. By "hiding" the code in a document based app that does the actual write(to: and Data(contents of:, it makes it impossible to tweak the code to work for your particular application. Not everyone wants to create another run of the mill document app. But if you can't see how the file is getting saved, how the URL is being pulled from the savePanel, how the data is being loaded from the file... you can't alter the code to suit your purposes. Luckily I was able to hodgepodge something together from a variety of internet tutorials, written by people who haven't completely given up on macOS... :[
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
AH! Okay, problem solved! I still had the old IBOutlet from the original AVPlayerView before I subclassed it. So now I have removed that link, and instead hooked up my customAVPlayerView, and using that as the reference I am able to pass data (via protocol / delegate) to my VC. THANK YOU!!!! I somehow missed the chapter on all the ways one can pass data between two classes. I kind of find the protocol / delegate thing to be a bit of a PITA. Maybe there are better and easier ways to do this? Thank you again!
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
I just removed the code that subclassed AVPlayerView, and set the identity inspector back to nothing. Now my view IS loading. So it's definitely the fact that I'm subclassing AVPlayerView. I moved all the subclassing code to another file. Now when I ask in viewDidLoad to print(customPlayerView) I get... <Player01.CustomAVPlayerView: 0x10100ee00> !!! We're getting close !!! print(customPlayerView.keyboardDelegate) is also printing Optional(<Player01.ViewController: 0x100631bf0>), so that seems to be instantiated at least. However... data is still not being passed to my delegate function in VC. I inserted this line in my keyDown function... print("from CPV: \(self.keyboardDelegate as Any)") and it is printing nil. So even though my class is being instantiated, and the delegate set on the VC side... do I have to init the delegate in my custom class?
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
I just created the project the normal way... xcode 12.3 on catalina 10.15.7. I just opened another app I made the other day and the view does load there. The only thing I can think of is that I have two classes in the same file. My ViewController swift file has a View Controller class, and I'm also subclassing AVPlayerView in the same file, as my code shows. I don't know if that's the right or wrong way to do it. I'm not using any old tutorials.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to delegate / protocol not receiving messages, help!
It does not print anything. I tried changing it to... var customPlayerView = CustomAVPlayerView() override func viewDidLoad() {     super.viewDidLoad() print(customPlayerView)     customPlayerView.keyboardDelegate = self } also prints nothing. But how is "var customPlayerView = CustomAVPlayerView()" not instantiating the class?
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to how to disable J,K, & L keys in AVPlayerView?
Ah, figured it out. I was close. You do subclass AVPlayerView. You must include this bit of code: override var acceptsFirstResponder: Bool {       get {         return true       }     } and the big one is you then have to set in the storyboard the class in the identity inspector to this new custom class. Then... code such as... override func keyDown(with event: NSEvent) {     if self.superview != nil     {      if event.keyCode == 124      {       print("right arrow")      }     }    } works! :]
Topic: Media Technologies SubTopic: Audio Tags:
Dec ’20