Tech notes for ios

Swift 3 and iOS: Using a UIAlertController

An UIAlertController is a alert popup, to which you can add buttons and text, and sub views if you wish. It can also act as a bottom sheet.

You create it programatically with:

let alert = UIAlertController(title: "Do something", message: "With this", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "A thing", style: .default) { action in
    // perhaps use action.title here

The addAction method adds a UIAlertAction which will act like a button. You can give it various styles, as with the alert controller above. The final anonymous function is called when this action is pressed.

Finally, you show it with self.present(alert, animated: true).

ios swift ios-uialertcontroller

iOS: Xcode 8 and UITabBarController

This is almost exactly the same as creating a UINavivationController.

First create a normal view controller in your story board, in the left hand pane, click on the icon that represents the view controller, and in the menu bar, under Editor, choose Embed in and choose Tab Bar Controller.

You'll now have a new tab bar controller in your storyboard, with a relationship view controllers to your existing view controller.

Create another normal view controller, and from the tab bar controller control drag to this new one, and create a view controller relationship segue.

In each view controller there'll now be a UITabBarItem which allows you to give your tab a title, icon and badge.

ios ios-tabbar

Swift 3, Xcode 8 and iOS: UINavigationController basics

The UINavigationController allows you to navigate back to a view controller from child view controllers using a back button.

First create a UIViewController with a button. Then select this in the left hand side hierarchical pane, then click Editor in the window menu, Embed in and then choose Navigation Controller.

They'll be a new UINavigationController, with a root view controller relationship link from that to your first view controller. They'll be a styleable UINavigationBar within this UINavigationController.

Create another view controller, and control drag from the button you created button in your first view controller to this new view controller, to create a show action segue.

On starting the app, it will load the UINavigationController (or should do, depending how you've setup your app), which will the start the first view controller. From there, when you press the button it'll load your third controller.

You should of course see a navigation bar at the top of each view. And when you get into the third view, you should see a back button you can use.

In your initial view controller, they'll also be a UINavigationItem, which you can use to set the title of your view controller and the name of the back button used when coming back.

Also, at the same level as the view in the third view controller, you can add UINavigationItem and give that a title too. And in of your view controllers, unclicking Adjust scroll view insets may help if your content is being pushed down.

swift ios ios-navigationcontroller

Swift 3 and RxSwift: Wrapping a URLRequest in RxSwift.

Let's assume we have request called r, let's first create an Observable:

let ob:Observable<Something> = Observable.create { observer in
    let task = URLSession.shared.dataTask(with: r) { data, response, error in 
    return Disposables.create {
.subscribeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .background))

We're starting the task in our observable, and returning a Disposable that simply cancels our task. And we're subscribing the a scheduler suitable for background tasks, and observing in our main thread.

These are the bare bones of our RxSwift stuff, but we're not actually doing anything with the task, we're not returning either onNext or an error. Let's do that:

if let e = error {
} else if let r = response as? HTTPURLResponse {
    if(r.statusCode >= 200 && r.statusCode < 300) {
        if let d = data {
            observer.onNext(Response(data: d, response: r))
        } else {
            observer.onNext(Response(data: Data(), response: r))
    } else {
        observer.onError(ResponseError(data: Data(), response: r))
} else {
    observer.onError(ResponseError(data: Data(), response: HTTPURLResponse()))

Firstly, we're looking for an error, and if there's one, we return that, otherwise if we have a response, we look at the status codes, and return as appropriate.

Response and ResponseError are simple data objects, and you probably want to do something with Observable.onComplete.

This is mostly stolen from [here] (

swift ios swift-rxswift

Swift 3 and iOS: Segues

Let's say you've control click dragged from a UIButton to another UIViewController in your storyboard. You've now created a segue.

Click on it, and in the attributes inspector tab, name its identifier segue1.

In your view controller from which you're leaving for the new one, add this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "segue1") {
        let detailVC = segue.destination as! SomeViewController;
        detailVC.someValue = "JOHN COLTRANE"

This says for the identifier segue1 which we named above, cast the UIViewController to SomeViewController, the name of our destination view controller.

Then set a property on that view controller, named simply via var someValue = "blar" for example in your destination controller.

Note, as soon as you have detailVC in the example above, the view controller will be initialised and visible on the screen.

ios swift

Page 3 of 7
prev next
Click me