Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does styling UI objects in iOS violate MVC?

I'm somewhat new to MVC and iOS development, and I can't seem reconcile how UI styling fits into this paradigm.

My view of MVC is built using storyboards, and I can apply primitive styling through Xcode's attribute inspector, but anything more complicated I have to use the Controller to style. For example:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; // required

    // set background color of view
    [[self view] setBackgroundColor:[UIColor darkGrayColor]];
}

This seems to be a clear violation of MVC, as I'm applying style logic inside of the controller's code. I find this analogous to writing an HTML app and instead of using style sheets, I write code to apply styles locally in JavaScript. Is this a weakness of iOS or am I just doing it wrong?

like image 648
Erich Avatar asked Oct 21 '22 05:10

Erich


2 Answers

Actually the screen is your "view", and your "controller" is sending a message to your view to use a different color for the background.

If you had a data object that held the screen color, that could be your "model". In that case, you'd be passing the data from your model to the view through the controller.

like image 29
DJ Spiess Avatar answered Oct 24 '22 04:10

DJ Spiess


Taken from Apple's docs :

some controller objects might also tell a view object to change an aspect of its appearance or behavior

And it does make sense as the view is supposed to be passive and only reflect the application state as a UI and the controller will "tell" the view if some of its content needs to be changed according to user actions. (e.g background change, visibility of controls etc...)

like image 199
giorashc Avatar answered Oct 24 '22 04:10

giorashc