For years, the only language for developing UI applications for OSX and iOS was Objective-C and the only IDE was Apple’s Xcode. Apple announced Swift at WWDC a year ago and Swift 2.0 at this year’s WWDC. Swift has a much easier syntax to learn than Objective-C, and is more secure and less prone to latent bugs like stack overflow errors.
There are at least three ways to develop GUI applications for iOS and OSX.
You can use a traditional text editor (like vim or emacs) to edit your source and use the command line compilers to compile and build your application. If you’ve ever set up a continuous integration system, you’re familiar with the command line tools.
Apple’s Xcode is a full featured IDE for developing C, C++, Objective-C, and Swift applications. The editor has its own idiosyncrasies, like hitting the tab key with a range of lines selected replaces the lines with a tab character instead of the expected behavior of indenting the lines. The interface for setting the various options for building and linking your program can be overwhelming.
Xcode has a Storyboard editor built in. This editor allows you to design the user interface for your app with a drag and drop WYSIWYG view and form based entry to configure the properties of the user interface elements. There are point and click features for connecting events to methods in your classes, though you will be switching between source code for your classes and the WYSIWYG editor to do so.
There’s no requirement that you use Storyboards at all. The alternative is to instantiate the UI element classes and to configure the elements’ properties in code. In doing so, you may dynamically layout your screen based upon screen dimensions and business logic that dictates elements be hidden or shown.
In this post, we will demonstrate how to create a Swift project in JetBrains’ AppCode, eliminate the Main.storyboard, and generate a view with a button that is centered on the screen horizontally. We won’t need to use Xcode at all.
The first thing we need to do is create our project in AppCode. Selecting “File”, then “New Project” brings up the following dialog. Note: we choose iOS Application and Single View Application.
Now that the project is created, we can run the default application generated by AppCode to see what it does. From the Run menu, choose Run, and then select a simulator or device target on which to run.
The application will only show a blank black screen because we have not specified a View to be displayed. We’ll need to edit the AppDelegate.swift file to set the window and its rootViewController. We set the window member to a created UIWIndow element. There is always one UIWIndow per application unless your app will display on an external device screen.
AppCode generated a ViewController.swift file for us that defines a default ViewController that basically does nothing. We had a blank white screen, after all. We set the rootViewController to this ViewController.
We also set the window’s background color to white and made the window visible.
Next, we’ll create a UIButton. The buttonPressed method of our ViewController will be invoked when the button is pressed. We instantiate the UIButton, and set its title, color, and position. Then we added it to our view (self.view). The button’s position and size are hard coded, which is often what you’d do with the Storyboard Editor in XCode.
AppCode has come a long way and is moving towards being the only tool you need to develop OSX and iOS applications. While it is not 100% there yet, it is just a matter of time before you won’t need to use the Xcode GUI at all.
For now, we can use AppCode, almost exclusively, to build entire applications. This gets you JetBrains’ code formatting, inspections, refactoring, key bindings, snippets, and other great tools, during development.
The idea of dynamically generating the screens is quite powerful. You can detect tablet vs phone and generate very different layouts for each. You can detect large screen phone (6+/6s+) vs small screen phone (iPhone 4) and render accordingly.
You can find the code for the example application on GitHub.