Friday, January 31, 2020

SwiftUI for Mac

Sarah Reichelt (via Ole Begemann):

So far, nearly all the articles I have seen about SwiftUI show it being used for iOS, more particularly for iPhone. But SwiftUI works on all Apple’s platforms, and as I am primarily a Mac developer, I decided to try out a Mac app and see what happened.

[…]

If I changed the structure of the view, the app closed and re-opened immediately with the new content. This is amazing and shows that the Xcode & SwiftUI teams really thought about how to use these new features in Mac apps as well as iOS.

Sarah Reichelt:

I had already discovered that the menu bar is configured in the Main.storyboard file.

[…]

In this case, it would make more sense to have the DetailView respond to the menu, since it contains the image. But trying to add an @IBAction to a SwiftUI View struct gives this error: Only instance methods can be declared @IBAction so I am using the App Delegate instead. I feel like this is a problem I need to solve, or the AppDelegate is going to end up filled with @IBActions, but for now, let’s keep going.

[…]

Not all the expected user interface elements are available in SwiftUI and some of the ones that are implemented are using different names. Again this is a point at which iOS development has got all the attention, so there are conversion tables out there listing UIKit elements and their SwiftUI equivalents, but I was not able to find a similar listing for AppKit. If you find one, please let me know.

[…]

And I was not able to set a button as the default button and I could not work out how to set a keyboard equivalent for a button.

Sarah Reichelt:

In this third and final part, I want to look at the various ways to present dialogs to the user.

[…]

To make an Alert, I need an @State Bool which sets whether the alert is visible or not. All the button has to do is toggle that Bool.

[…]

The trick to dismissing the sheet is to pass it a Binding to the Bool that triggered it to open in the first place. If a button in the sheet sets this Bool back to false, the parent view will hide the sheet. That sounds confusing, but it works.

Sarah Reichelt:

I would like to thank everyone who contacted me about this series. It was received very well and revealed that there is still a large amount of interest in programming for the Mac.

Some of the responses I got were pointing out different or better ways to do things, so I am going to list them here, adding to this post as I get new information.

See also: SwiftUI First Thoughts and SwiftUI Data Flow.

Comments RSS · Twitter

Leave a Comment