The MVP (Model-View-Presenter) pattern is a design pattern used to separate the user interface logic from the business logic in applications. MVP helps make code easier to maintain and test. In an iOS application using Objective-C, you can organize the MVP pattern as follows:
Key Components of MVP:

1. Model

Contains the data and business logic of the application.
 
Student.h
@interface Student : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
@end
 
Student.m
@implementation Student
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
self = [super init];
if (self) {
_name = name;
_age = age;
}
return self;
}
 

2. View Protocol

The View protocol will include methods to display data and may have methods to show errors or other statuses.
The presenter communicate to the related view through view protocol
 
StudentViewProtocol.h
 
@protocol StudentViewProtocol <NSObject>
- (void)showStudentName:(NSString *)name;
- (void)showStudentAge:(NSString *)age;
@end

 

3. Presenter

The Presenter communicates with the View via the StudentViewProtocol. Additionally, the Presenter needs a protocol for the View to notify user events.
 
StudentPresenter.h
 
#import "StudentViewProtocol.h"
@protocol StudentPresenterProtocol <NSObject>
- (void)onLoadStudentButtonClicked;
@end
 
@interface StudentPresenter : NSObject <StudentPresenterProtocol>
@property (nonatomic, weak) id<StudentViewProtocol> view;
- (instancetype)initWithView:(id<StudentViewProtocol>)view;
@end
 
StudentPresenter.m
 
#import "StudentPresenter.h"
#import "Student.h"
@implementation StudentPresenter
- (instancetype)initWithView:(id<StudentViewProtocol>)view {
self = [super init];
if (self) {
_view = view;
}
return self;
}
- (void)onLoadStudentButtonClicked {
// In a real application, you might fetch data from a service or database
Student *student = [[Student alloc] initWithName:@"Phuoc Nguyen" age:45];
// Format data before sending to the View
NSString *ageString = [NSString stringWithFormat:@"%ld years old", (long)student.age];
// Update the View
[self.view showStudentName:student.name];
[self.view showStudentAge:ageString];
}
@end

4. View

View contains only view-related logic
In iOS, ViewController acts as the View and is also the place where user events are notified to the Presenter.
StudentViewController.h
#import <UIKit/UIKit.h>
#import "StudentViewProtocol.h"
#import "StudentPresenter.h"
@interface StudentViewController : UIViewController <StudentViewProtocol>
@property (nonatomic, strong)id<StudentPresenterProtocol> presenter;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UIButton *loadStudentButton;
@end

 

StudentViewController.m
#import "StudentViewController.h"
@implementation StudentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.presenter = [[StudentPresenter alloc] initWithView:self];
[self.loadStudentButton addTarget:self.presenter action:@selector(onLoadStudentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
- (void)showStudentName:(NSString *)name {
self.nameLabel.text = name;
}
- {
self.ageLabel.text = age;
}
@end
 
In this example, the View notifies the Presenter when the "Load Student" button is clicked. The Presenter then processes the request, fetches data from the Model, formatsit, and updates the View. This ensures that all handling logic is separated from the View, making the code more maintainable and testable.
The class diagram:

(The post is used free image from Pexels source)

Leave a comment

*