0

Recently I was studying the possibility of creating multiple classes in only one file, for this I created a class of UIViewController with a .xib file, the structure of the file is as follows:

MyFristViewController.h

#import <UIKit/UIKit.h>

@interface MyFristViewController : UIViewController

@end

@interface MySecondViewController : UIViewController

@end

MyFristViewController.m

@implementation MyFristViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Frist View Loaded");
}

@end


@implementation MySecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Second View Loaded");
}

@end

My doubt is: How does the system know that is to perform the methods contained in the class called 'MyFristViewController'?

I already tried to modify the custom class in interface builder, tried to change the position of the classes in the file and the system continues running only the existing methods inside the 'MyFristViewController' class why?

LettersBa
  • 747
  • 1
  • 8
  • 27
  • 1
    Why not? It is clearly demarcated which class is which. File names are completely irrelevant; they are just a convenience to _you_. It is not at all clear what you are surprised at. – matt Mar 08 '15 at 21:24

3 Answers3

1

How does the system know that is to perform the methods contained in the class called 'MyFristViewController'?

The filenames are irrelevant. When looking at a class, for the most part, the code between @implementation <#ClassName#> and @end is used.

Additional customization of classes can be added through categories and class extensions. These can also be specified in the same file, or different files, because (again) the filenames are irrelevant.

Generally, you should have one class per file to make it easy to read and find your code. See How many classes should a programmer put in one file? for additional discussion.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

My doubt is: How does the system know that is to perform the methods contained in the class called 'MyFristViewController'?

Because the methods are in the @implementation block of MyFristViewController.

I already tried to modify the custom class in interface builder, tried to change the position of the classes in the file and the system continues running only the existing methods inside the 'MyFristViewController' class why?

Probably because you've linked them to the methods in first @interface section in your header file. Control-drag to the actual method you want to bind to. It's not clear what problem you're actually seeing.

That said, this is a terrible idea. Put each view controller in its own file. It'll work in one file, but it will create lots of confusion, as you're seeing.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

I have worked on projects that define multiple classes in a single file. I loathe this practice. I find it very disorienting and I waste a lot of time searching for where the classes are defined/implemented.

I would advise you not to do this. It ends up being very confusing.

Duncan C
  • 128,072
  • 22
  • 173
  • 272