Design Pattern - Objective-C - MVC Model View Controller

后端 未结 2 354
Happy的楠姐
Happy的楠姐 2021-02-03 15:20

Hi I already read tutorials around the web on MVC and already read the topics on here. I think i got the concept of the MVC but i\'m not sure of its implementation.

I\'v

2条回答
  •  北海茫月
    2021-02-03 15:44

    For whose have any doubt about where put the UI elements, I often like put the UI elements in View.m

    My strategy is make all methods to build UI elements in View.m, where I have a method that call all others methods in View.m. Thus, i call only one method in ViewController

    For example :

    TodayView.h

    #import 
    
    @interface TodayView : UIView
    @property (strong, nonatomic) UIImageView *imageView;
    @property (strong,nonatomic) UINavigationBar *navBar;
    
    
    -(void) addAllElements:(UIView*) mainView addController:(UIViewController*) controller;
    -(void) addImage:(UIImageView*) image view:(UIView*) todayView;
    -(void) addNavBar:(UIViewController*) navController addView:(UIView*)view;
    @end
    
    

    TodayView.m

    
    #import "TodayView.h"
    
    @implementation TodayView
    
    
    -(void) addImage:(UIImageView *)image view:(UIView *)todayView{
        image= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        image.image = [UIImage imageNamed:@"icone-apps"];
    
        [todayView addSubview:image];
    }
    
    -(void) addNavBar:(UIViewController *)navController addView:(UIView *)view{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 120, 50)];
           label.textAlignment = UITextAlignmentCenter;
           [label setFont:[UIFont boldSystemFontOfSize:40.0]];
           [label setBackgroundColor:[UIColor clearColor]];
           [label setTextColor:[UIColor blackColor]];
           [label setText:@"Hoje"];
           [navController.navigationController.navigationBar.topItem setTitleView:label];
           [view addSubview:label];
    
    }
    
    -(void) addAllElements:(UIView *)mainView addController:(UIViewController*)controller{
        [self addNavBar:controller addView:mainView];
    
    }
    
    @end
    
    
    

    TodayViewController.m

    
    #import "TodayViewController.h"
    @interface TodayViewController ()
    
    
    @end
    
    @implementation TodayViewController
    @synthesize myView;
    
    -(void) blankMethod{
    
    }
    
    -(void) addImage:(TodayView*)todayView{
        todayView.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        todayView.imageView.image = [UIImage imageNamed:@"icone-apps"];
    
        [self.view addSubview:todayView.imageView];
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = UIColor.whiteColor;
    
        TodayView *todayView = [[ TodayView alloc] init];
         # Here I call the method that call all others methods to build UI elements
        [todayView addAllElements:self.view addController:self];
    
    
    
    
    }
    -(UITabBarItem*) tabBarItem{
    
        return [[UITabBarItem alloc] initWithTitle:@"Hoje" image:[UIImage imageNamed:@"icone-hoje"] tag:0];
    }
    
    
    @end
    
    

提交回复
热议问题