Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it right having ViewControllers with a lots of code?

I'm still quite new to Cocoa and Objective-C (<1 year). My App now has some 50+ classes, but some of the ViewControllers get quite crowded with code, like 700 lines or more.

My question is: is it fine to have a "large" ViewController or are there patterns for splitting up code into fractions? A lots of the code is implementing delegate methods, thats why I don't have an idea how to move it away.

I know, I can structurize with pragma marks though.

Thanks for any input.

EDIT (Dec 2013): there is a great article from Chris Eidhof of objc.io regarding this subject. He also talked about that subject on Macoun 2013/Frankfurt. Separating out the UITableView protocols are a great pattern.

EDIT2 There are also 2 videos on NSScreencast explaining concepts of refactoring ViewController (episode #102 and #103).

like image 874
brainray Avatar asked Dec 09 '11 10:12

brainray


People also ask

How to add view to ViewController?

Add views to your view controller In storyboards, you add views by dragging them onto the view controller scene.

How do I link my storyboard to ViewController?

Create a new IBOutlet called shakeButton for your storyboard button in your ViewController. swift file. Select the shake button in Interface Builder. Then hold down the control button ( ⌃ ) and click-drag from the storyboard button into your ViewController.

What is view controller in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.

How do I change the view in Xcode?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.


2 Answers

One of the most common causes of large view controllers that I have seen, is lack of separation b/w Model and Controller in MVC architecture. In other words, are you handling your data in your view controllers?

If yes, rip out the model component from VC and put it into separate class. This will also force your thinking towards a better design.

For reference, In View Controller:

  • Handling of all changes in the UIView and the UI elements contained within.
  • All animation, transitions and CALayer operations.

In Model:

  • All processing of data, including sorting, conversion, storage, etc.
like image 118
Sanjay Chaudhry Avatar answered Oct 18 '22 11:10

Sanjay Chaudhry


IMHO, 700 lines is not (yet) huge for iOS code, I've seen and handled much worse. Of course, if all your VCs are this big, you have a problem.

You should definitely use and abuse #pragma mark, it's very useful under Xcode at least.

Then if you find you got too much code in one file, you can extract functionality to classes or categories, as better fits.

Creating classes can be very rewarding in the long term to manage recurring tasks in projects (ie connecting to webservice, parsing XML/JSON, interacting with SQLlite, logging, etc...). If you are doing recurrent iOS programming, you can create a 'common' library of useful code that way.

Creating categories, especially on UIViewController can help reducing boilerplate code that takes a lot of place. You can (and probably should) also create a common base UIViewController for your app, that will handle stuff like rotation, maybe logging, navigation, etc... in a centralized part of the code.

like image 20
jv42 Avatar answered Oct 18 '22 10:10

jv42