Break up massive view controllers (Swift)

Introduction

MODEL-VIEW-ADAPTER DESSING PATTERN. It is quite common in iOS programming that view controllers begin to grow and at the middle of development you discover that you have a view controller class with hundreds or even thousands of line. This is the common scenario where the view controller, apart from dealing with its controllers (uitableviews, uitableviews, hierarchy of views,…), has also to process data for being properly presented attend to notifications.

 

The aim of this post is to explain an approach for breaking up view controllers into different design classes and provide them different responsibilities for practically leave the view controller with the responsibility of setup its view controller and filling its ui controllers when necessary.

Class design

MVC approach

MVC

This approach is the most convenient case that the amount of information that you have to present in the view is quite reduced.

MVA approach

mva

 

This other is the most convenient when you have to deal with more than one dataSource in the same view controller, and also the origin of the data to show come from different sources (a data base, internet request, some costing data process,…).

Responsibilities:

View. As always, show the content views.

View Controller. In this approach, setup its uicontrols (tableviews, collection views, a view hierarchy, …) and attend its events. This is not something new, but what is new is that is responsible of being delegate of an Adaptor class and implement its delegated methods. Those delegated methods are basically informing view controller that there is new data to show in the view.

ModelView. Is the model data to be shown in the view.

Adaptor. Is the responsible for starting the processes for retrieving data and providing it to Model Data. Once there is some data to show is provided to viewcontroller via delegate.

AsyncOperation1,…n. Are asynchronous operation for retrieving data from elsewhere. In the worst case scenario this action will take time.

As you can see the View Controller get slimmer by moving part of the code that it performed in MVC approach to Adaptor and AsyncOperation classes.

The template

For proving you that concept you can take this sample project that implements what I have exposed.

 

 

Custom uitableview cell in Swift

The aim of this post is to show how to build a Custom SWIFTed TableViewCell view cell. I have found several shallow examples with the default cell. But soon you will discover that will not fit a real sample project situation. I have found several difficulties from moving to Swift, that took me more time than I expected and that pushed me also to write this post.

The custom table view cell

I have just created a new Single View Project Application  in the default view controller  and added a new NSObject class that extends from uitableviewcell.

Captura de pantalla 2015-07-10 a las 18.54.54

 

Captura de pantalla 2015-07-10 a las 18.56.24

 

The storyboard

I have added a table view controller and a tableview cell with an image view and a label.

Captura de pantalla 2015-07-10 a las 19.02.50

Now is time to setup the custom table view cell. Firstly the  new  class associated to the cell:

Captura de pantalla 2015-07-10 a las 19.07.15

And secondly set the cell identifier:

Captura de pantalla 2015-07-10 a las 19.07.40

The View Controller

Now it is time for connecting the storyboard with the source code. Lets start off with the main view controller:

I have mapped the tableview to tbvData, and added the minimum uitableview delegates. The table view is filled with 3 elements contained in two arrays (items and images).

The table view cell

In the custom cell I have added an image view and a label as an example. This is the code that  configures the cell:

 

Finally

Captura de pantalla 2015-07-11 a las 14.58.34

You can get also find the code here

Singleton dessing pattern in a template (Swift)

The aim of this post is just complement Singleton design pattern in a template post , but in Swift.

Singleton implementation class

 

Not that in Swift ‘dispatch_once’ is no longer necessary, as in Objective-C. Static constants in Swift, internally does it. So this is an issue that you do not have to worry about.

Template usage

This is how the pattern is used.

 

Conclusion

As you can see Swift language structure facilitates a lot Singleton design pattern implementation. You can get the project source code used for writing this post here.

Image animations on Watchkit sdk

The aim of this post is just showing how to do a simple image animation. I have been investigating if it was possible to make animations in Watchkit using CALayer class, as in normal iPhone and iPad apps, but it has not been possible. The possibilities that provides UI in the Watchkit are very very poor, I figure it out that apple guys are fixing that.

The base project

For setup the base project just follow the instructions from a previous post. Add in the main scene the image that will hold the animation.

Image animations on Watchkit

Link the image to an IBOutlet.

IBOUTLETImage animations on Watchkit

 

Now we will add the images used for the animation:

Image animations on Watchkit

Add the following lines to the watch kit interface view controller:

And Finally run the target:

Image animations on Watchkit

If the instructions were followed you should have to see something like this:

Image animations on Watchkit

 

If you had any doubt about what I have explained, you can find this project on Github.

Disable fullscreen mode on MPMoviePlayerController

The aim of this post is explain how to disable fullscreen mode  on MPMoviePlayerController. From the beginning you could think that there would be a flag to control that, but the reality is quite different. Since apple guys do not put hands on it, the rest we will have to deal with workarounds like this one.

The problem

My problem was that I wanted to embed a movie player in a scene, but movie player showed the fullscreen button mode. The aim of my scene was allow to play a movie in a part of the scene. Googling about this matter I found that this problem was known, but none of the solutions given worked fine to me, that’s why I want to report my personal solution.

The solution.

The solution consists in two independent actions. The first one is track all Movie player subviews, and once found the one that contains the fullscreen button view just hide it.

Add this chunk of code in the place where you initialise your player:

The call must be deferred because until the movie player is not presented, its sub collection of views is not built.

As movie player view is a tree of subviews, the unique I had to track this structure by using a recursive call:

A clarification, movieclip is an instance of MPMoviePlayerController. Since there is no identifier tag for the view I had to check its properties to know which view I had to hide. In my case was the view coordinates, it does not sound professional solution, but after days breaking stones I did not something better.

The second one is overwrite tap actions on movie player, because doble tap does a full screen. Also add the following chunk of code in the place where you initalise the movie player.

And its selector methods:

… for doing nothing.

If all was implemented as described, you should have to see that it has disappeared fullscreen icon and double tapping on video does not do anything at all.

Disable fullscreen mode on MPMoviePlayerController

Disable fullscreen mode on MPMoviePlayerController

This solution is also explained in Stackoverflow .