2

I have been studying the iOS app structure, and almost got my head around the MVC paradigm, with the delegates, views and controllers. Then I came across an example on the net that comprised of just one AppDelegate file, with no views, view controllers, or nib files, and it works:

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];

        CGRect bounds = self.window.bounds;
        view = [[UIView alloc] initWithFrame: bounds];
        [view setBackgroundColor: [UIColor yellowColor]];
        [self.window addSubview: view];

        CGRect labelFrame = CGRectMake(30, 30, 100, 30);
        label = [[UILabel alloc] initWithFrame: labelFrame];
        [label setText: @"My Label"];
        [label setTextAlignment: NSTextAlignmentCenter];
        [label setTextColor: [UIColor orangeColor]];
        [view addSubview: label];

        CGRect buttonFrame = CGRectMake(vCentre - 50, hCentre - 20, 100, 30);
        button = [[UIButton alloc] initWithFrame: buttonFrame];
        [button setTitle: @"My Button" forState: UIControlStateNormal];
        [[button titleLabel] setTextAlignment: NSTextAlignmentCenter];
        [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
        [view addSubview: button];

        [button addTarget:self action:@selector(buttonClicked:) forControlEvents: UIControlEventTouchDown];

        [self.window makeKeyAndVisible];

        return YES;
    }

How is this working without any view controller or root view controller set? Does this mean that view controllers aren't really required?

iSofia
  • 1,412
  • 2
  • 19
  • 36

5 Answers5

1

You can design an application in Interface Builder and connect all the outlets. Or you can design the windows, labels, etc. in code.

This creates a window filling the screen:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

Then you add a view to it:

[self.window addSubview: view];

And a label:

[view addSubview: label];

Finally, show the window:

[self.window makeKeyAndVisible];
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • I'm slowly getting that. So the AppDelegate is the default controller? And setting the view controller or root view controller is just a method to pass that control to another module? – iSofia May 13 '15 at 14:36
  • 1
    Eh, I thought the OP asked if they are _necessary_ on the app, not "how does this code works?". But whatever. – Gabriel Tomitsuka May 13 '15 at 14:36
  • @GabrielTomitsuka Knowing how it works is key to knowing why it is or isn't necessary. – iSofia May 13 '15 at 14:41
  • 1
    @iSofia you are correct. The App Delegate is the first object called by iOS. It then *optionally* assigns the control of each UI element to a view controller. In your example, the App Delegate acts as the view controller itself. Really, a view controller is simply something that has a reference to your UI elements. – Code Different May 13 '15 at 14:44
  • @ZoffDino: You're right - AppDelegate is called by main. And setting the view controller is simply a way of passing that control over to another module. What is the root view controller then? (I would love to vote for your answer) – iSofia May 13 '15 at 14:49
  • 1
    @iSofia there is no root in a strict sense. Each view control minds its own business and knows how to call others that are related to it. An example is you have a list of 5 items, you tap on one, and the List View Controller will call the Detail View Controller to display the details of the tapped item. The Detail View Controller then remembers the List View Controller when you tap "Back". – Code Different May 13 '15 at 14:54
  • @ZoffDino: I think I've got it. But one more clarification, please: then what is the rootviewcontroller setting for? – iSofia May 13 '15 at 14:57
  • 1
    @iSofia this should answer your question: http://stackoverflow.com/questions/5842087/programming-ios-clarifications-about-root-view-controller – Code Different May 13 '15 at 14:59
1

While it's not required to use view controllers, they make up for a very logical division of your application. They also facilitate the use of storyboards to allow graphical design of your app's user interface.

This particular example just draws a couple of elements on the screen, with no interaction possible at all.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
1

As you can see here, the developer behind this is creating all the views directly from the code. You can do that, as you can do all the logic in one view controller without any models. As the matter of fact you can create the application as you wish. You can create whole app using html and css inside your web views if you want. There is no restrictions in the objective-c or any other language for that. If you want to violate any principle in your code you can do that.

That's where the best practices come in. You shouldn't create everything in App delegate, you shouldn't have only one storyboard(my opinion for work in team), basically you should use something that is proven to be a good way to develop and manage your app.

One more thing, note that this code creates only one view and one label. This code would be really really big if you wanted to create WHOLE app in your function. Imagine thousands and thousands of lines and you have to find a bug. That would be really really hard.

Miknash
  • 7,888
  • 3
  • 34
  • 46
1

MVC is just as you said, a paradigm. It is a way to set up your code so that it has good cohesion and low coupling. You logically lay out your code so that business logic is in the model, display is in the view, and connecting stuff is in the controller. Apple pushes this really hard with their code, but technically, only the view is required for an app, as you still need to display stuff to the screen.

This code just creates the window and then adds a view to it, which displays some content that has been set up programmatically. It works but is not good practice, as you don't want your view code to be coupled to your app delegate.

Will M.
  • 1,864
  • 17
  • 28
1

Yes, UIViewControllers aren't required at all!

You can just pick some SVGs for using as background and add some buttons or UIWebViews for adding functionality and other UIKit elements.

Keep in mind that this is an unrecommended and hard-to-maintain practice. If you have tens of views, it might be a good idea to start using UIViewControllers. Other people reading your code will have a hard time trying to understand it and no one will really end up happy with it.

Gabriel Tomitsuka
  • 1,121
  • 10
  • 24