How to put "M" in MVC using Interface Builder

I agree with the answers given in What is the best way to communicate between view controllers? but what if I use Interface Builder to develop my Views app?

Without using an application delegate, how can I refer to model objects from Controller objects?

As far as I can tell, I can't put Init user methods that use dependency injection to pass model references to IB.

Forgive me, because I feel that I am missing something very basic here. I looked at the Apple documentation for IB but did not find a clear answer.

+3
source share
3 answers

In the article, your recommendation is good - like you, I like to enter the model into the controller (an old habit from Smalltalk days - in particular, Dolphin Smalltalk follows this pattern quite widely).

I found that most of my controllers have init methods, such as:

- (id)initWithItem:(id<Nameable>)item addingTo: (id<ModelContainer>)model {
    if (self = [self initWithNibName:@"ItemEditTableToolbarView" bundle:nil]) {
        self.namedItem = item;
        self.container = model;
    }

    return self;
}

Then you can call your controller as follows:

ReDoListEditController *itemViewController = [[ReDoListEditController alloc] initWithItem: item addingTo: model];
    [self.navigationController pushViewController: itemViewController animated: YES];
    [itemViewController release];

, Nib init ( , , , - . - Dolphin, ( ) - init init . , iPhone IB, .

, ( , ).

  • XCode ( , )
  • "YourAppDelegate.m" applicationDidFinishLaunching: - :

    viewController = [[AppRootViewController alloc] initWithModel: [ ]]; viewController.view.frame = [[UIScreen mainScreen] applicationFrame];

    [window addSubview: viewController.view];

    [ makeKeyAndVisible];

, , ( ):

  • MainWindow.xib( , MainWindow - , IB)
  • .plist MainWindow.xib( ).
  • main.m UIApplicationMain ( ) - :

    int retVal = UIApplicationMain (argc, argv, nil, @ "YourAppDelegate" );

  • "YourAppDelegate.m" applicationDidFinishLaunching: - :

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

    viewController = [[AppRootViewController alloc] initWithModel: [ ]]; viewController.view.frame = [[UIScreen mainScreen] applicationFrame];

    [window addSubview: viewController.view];

    [ makeKeyAndVisible];

UIApplication:

- (MyModel *)model;

( ), , .

viewModel = [[UIApplication sharedApplication] model];

, , , , , .

+3

InterfaceBuilder V MVC. , IB, . - IB . C MVC , .

+1

IB. , , . ( ) . awakeFromNib, , init .

0
source

Source: https://habr.com/ru/post/1710115/


All Articles