Build Your First MacOS App

We’re missing the core experience of the whole application, right now why should someone download an alternative clock with no extra features?So let’s add the reminders feature, it’s pretty simple, and you’ll learn how to create and present a window.

We can start creating a new struct type and call it ReminderThen create a new variable of type [Reminder] and add empty array as default value.

var reminders: [Reminder] = []Now we can write some logic stuff:Add a MenuItem to the StatusMenuAdd a submenu with all the remindersAdd a MenuItem that opens a new window to schedule a new Reminder.

Delegate reminder and the view controller in theAppDelegateCreating the NSMenuItem and its submenuAs we have already done before for the other elements, let’s create a new NSMenuItem and this one like the statusBarItem will have a menu property, this will be populated with all the reminders inside our reminders variable declared before.

How can I add a new Reminder?Now we need to create an interface to add reminders, to do that open your Main.

storyboard and create a view controller with an NSTextField for the title, NSTextView for the description and NSDatePicker for the date.

You can do this also from code, programmatically, but since this article is becoming really long I’ll show you the fastest way to archive a basic window with controls.

My interfaceOk now let’s create a new file, this will be our view controller, in case you haven’t delete it yet, you should have a file named ViewController, you can edit this one instead of create a new file.

Rename it to NewReminderVC and rename also the class inside, you should have something like this one below:To save time I’ve just added a new Delegate Protocol on top of the file, this will be used to handle the submit event.

Okay, you’re all set, go in your storyboard, select the view controller you’ve just created and assign this new class to it:Identity InspectorMake sure to add a Storyboard ID, for practicality I name it just like the view controller class.

We’ll use the Storyboard ID to identify our view later in the code.

Now with the view controller selected, click on the Assistant Editor button on the top right corner of Xcode.

This will split your screen in 2 editors.

Make sure to have the right file selected (the right file is the file of the view controller), then drag and drop (with ctrl pressed) your items in the class.

You should have 3 IBOutlets and 1 IBAction, the action is connected with the button, the other 3 outlets are the title, the description, and the date picker.

Ok, now you have a view but how can we present it?.Easy, create a new file with this helper, this will give you a function named getVC you need to pass the Storyboard ID and the view controller classWindows HelperOk now let’s add the missing function in the AppDelegate.

The first thing to do is declaring a new constant on top of the file:let REMINDERS_WINDOW_CONTROLLER: NSWindowController = NSWindowController(window: nil)All right, now we miss an action, let’s add some stuff:Firstly we need to edit our reminders variable and add a didSet event:I’ve created a utility function to easily create the reminders menu, now let’s create the menu item, and don’t forget to add it to the menu.

Now the addReminder it’s still not declared so Xcode will alert you with an error, let’s fix that by declaring it:To understand floating level read below:See Window Levels for a list of possible values.

Each level in the list groups windows within it in front of those in all preceding groups.

Floating windows, for example, appear in front of all normal-level windows.

When a window enters a new level, it’s ordered in front of all its peers in that level.

Last but not least, we need to implement delegates and do something inside the NewReminderVCIn the viewDidLoad method we set our default values, then onSubmit we create our new reminder and then we call the onSubmit method of the delegate.

????.DelegationFinally, we need to conform with the delegation from NewReminderVC, to do that add an extension of AppDelegate like the following:ResumeYou can find the entire project on my github at Advanced Clock.

Now you should be able to run your application.

These are the basics for Mac Development, or at least what I’ve learned in the past weeks by searching on Google and StackOverflow.

Below I leave you some articles here on Medium about some fundamentals conceptsImplementing delegates in Swift, step by step — James RochabrunEpisode #1.

Storyboards — Harry NgThank you for reading, I hope that this has been useful, don’t forget to visit my Twitter and GithubRawnly – OverviewReact developer.

Now focused on Swift.

Rawnly has 126 repositories available.

Follow their code on GitHub.

github.

comFederico Vitale (@fedevitaledev) | TwitterThe latest Tweets from Federico Vitale (@fedevitaledev).

????.Developer – Swift / NodeJS.

Roma, Laziotwitter.

com.. More details

Leave a Reply