-1

When I work in Objective-C programatically with out nib files, and have the logic in my:

appViewController.m

having in the same class What is going on with that view, as well as with the View elements? Is this against the MVC pattern?

Do I have to create another class and message both classes?

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
  • 3
    can you please reformulate your question in more clear manner? – Denis Dec 02 '11 at 12:50
  • As written, this question is far too vague. Nib files have very little to do with MVC in general, and you haven't told us what "appViewController" does or what "logic" you're talking about. – Caleb Dec 02 '11 at 15:49

2 Answers2

1

It's up to you! If you want to separate layers (M,V,C) you can create your own view programmatically and, by using composite design pattern, build it in your UIView subclass, by removing drawing code from your controller. That is...

  • You create a "CustomCompositeView" that extends UIView
  • in layoutSubview (hinerited from UIView) you will draw all your UI elements
  • in your CustomViewController you will display your view using loadView:

code:

- (void)loadView
{
CustomCompositeView *mainView = [[CustomCompositeView alloc] initWithFrame:aFrame];
[self setView:mainView];
[mainView release]; // remove this line if you are using ARC!
}
daveoncode
  • 18,900
  • 15
  • 104
  • 159
1

Technically, it is going against the MVC pattern. Your V and C and combined into a single object. You can seperate the code that handles layout and drawing into a seperate UIView subclass. Then load it with loadView:

// MyViewController.m

- (void)loadView {
    MyView* myView = [[[MyView alloc] init] autorelease];
    myView.delegate = self;
    self.view = myView;
}


#pragma mark - MyViewDelegate Methods

- (void)myViewSaveButtonWasPressed:(MyView *)myView {
    // do something
}

To communicate between the view and the view controller, you can define a delegate protocol.

// MyView.h

@class MyView;
@protocol MyViewDelegate <NSObject>
- (void)myViewSaveButtonWasPressed:(MyView *)myView;
@end

@class MyView : NSObject

@property (nonatomic, assign) id<MyViewDelegate> delegate;
// ...

When a button is pressed in the view (or something else along those lines) pass that on to the delegate. The ViewController should conform to the delegate method and handle the actual logic itself that way.

Cam Saul
  • 1,706
  • 1
  • 16
  • 24