1. Model
@interface Student : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, assign) NSInteger age;- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;@end@implementation Student- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {self = [super init];if (self) {_name = name;_age = age;}return self;}2. View Protocol
@protocol StudentViewProtocol <NSObject>- (void)showStudentName:(NSString *)name;- (void)showStudentAge:(NSString *)age;@end
3. Presenter
#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#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 databaseStudent *student = [[Student alloc] initWithName:@"Phuoc Nguyen" age:45];// Format data before sending to the ViewNSString *ageString = [NSString stringWithFormat:@"%ld years old", (long)student.age];// Update the View[self.view showStudentName:student.name];[self.view showStudentAge:ageString];}@end4. View
#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
#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;}@endThe class diagram:
