MVC模式和代理模式

6 篇文章 0 订阅

模式的思想很重要,一开始认为模式没那么重要,直接编写视图不就行了么。但是在编写大型项目的时候,管理就成了一个问题,所以一个模式就是一个管理方法。

mvc模式的思想就是把所有的控制都交给c来做,v基本不做处理。给v添加两个属性,一个是id类型的(名字叫target,也可以叫别人的名字),一个是方法类型(SEL),这样就可以在c给id类型的创建方法。在c中有V的实例变量(也可以是属性)。通过实例变量给v的属性传入方法和···

代理模式

代理模式以前挺容易东的,但是发现在不同的场合,理解起来是不同的 。尤其是根系统的类结合起来。就跟一开始练习的找保姆的代理理解高了一个等级。
1.设置协议
2.类中的一个属性遵守协议
3.在类中待处理的方法中使用代理,让代理使用方法处理。
4.需要代理的类遵守协议,并实现协议的方法

下面将两个模式写在了一起。

#import <Foundation/Foundation.h>
@class TouchView;


//当单独建立一个协议的时候,就用@class
//#import "TouchView.h"        会造成循环导入,       #include会造成重复导入       所以要用@class

@protocol TouchViewDwlegate <NSObject>


//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
//- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
//- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
//
//- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
//- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
//
@required
@optional        //协议中的方法可选实现
//开始触摸
-(void)touchViewTouchBegan:(TouchView *)touchView;


//边移动边触摸
-(void)touchViewTouchMoved:(TouchView *)TouchView;


//触摸结束

-(void)touchViewTouchEnded:(TouchView *)TouchView;

//触摸中断

-(void)touchViewTouchConcel:(TouchView *)TouchView;


@end
#import <UIKit/UIKit.h>
#import "TouchViewDwlegate.h"
@interface RootViewController : UIViewController<TouchViewDwlegate>

@end
#import "RootViewController.h"
#import "TouchView.h"
#import "UIColor+RandomColor.h"

@interface RootViewController ()

{
    TouchView *_touchViewOne;
    TouchView *_touchViewTwo;
}

//@property(nonatomic,retain)TouchView *touchViewOne;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    //第一个view
    _touchViewOne = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    _touchViewOne.backgroundColor = [UIColor yellowColor];
//    _touchViewOne.target = self;


    //加个冒号,就把touchView传过来了
    _touchViewOne.action = @selector(changeColor:);

    [self.view addSubview:_touchViewOne];


    //第二个view
    _touchViewTwo = [[TouchView alloc]initWithFrame:CGRectMake(100, 250, 100, 100)];
    _touchViewTwo.action = @selector(changeColor:);


    [_touchViewOne setTarget:self];
    _touchViewTwo.target =self;



    _touchViewTwo.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_touchViewTwo];


    //设置代理
//    _touchViewOne.delegate = self;



    //释放
    [_touchViewTwo release];
    [_touchViewOne release];
    // Do any additional setup after loading the view.
}


-(void)changeColor:(TouchView *)view
{
    view.backgroundColor = [UIColor colorWithRandom];
}


-(void)changePosition:(TouchView *)view
{

//    CGPoint temp = view.center;
//    
//    temp.x +=10;
//    temp.y +=10;
//    
//    
//    view.center = temp;
//    
//    
//    
//    
//    
//     view.backgroundColor = [UIColor colorWithRandom];
}




#pragma mark =============实现TouchViewDelegate.h中的方法



//开始触摸
-(void)touchViewTouchBegan:(TouchView *)touchView
{
    touchView.backgroundColor = [UIColor colorWithRandom];
}


//边移动边触摸
-(void)touchViewTouchMoved:(TouchView *)TouchView
{

}


//触摸结束

-(void)touchViewTouchEnded:(TouchView *)TouchView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];


    CGPoint temp = TouchView.center;


    temp.y +=30;


    TouchView.center = temp;
    [UIView commitAnimations];
}

//触摸中断

-(void)touchViewTouchConcel:(TouchView *)TouchView
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>
#import "TouchViewDwlegate.h"

@interface TouchView : UIView

@property(nonatomic,retain)id target;



@property(nonatomic,assign)SEL action;

@property(nonatomic,assign)id <TouchViewDwlegate>delegate;



-(void)addTarget:(id)target action:(SEL)action;

@end
#import "TouchView.h"
#import "UIColor+RandomColor.h"
@implementation TouchView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    self.backgroundColor = [UIColor colorWithRandom];
//    self.backgroundColor = [UIColor redColor];
//    
//    self.center = self.superview.center;

//    [视图控制器 执行 方法];


//    [_target  执行  _action];

      //代理执行协议中的方法
    //先去检测touchViewTouchEnded:方法有没有实现,如果实现,再让deletate执行这个方法
    if ([_delegate respondsToSelector:@selector(touchViewTouchBegan:)]) {
        [_delegate touchViewTouchBegan:self];
    }

//    [_target performSelector:_action withObject:self];





}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([_delegate respondsToSelector:@selector(touchViewTouchBegan:)]) {
        [_delegate touchViewTouchEnded:self];
    }



}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_delegate touchViewTouchEnded:self];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{

}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值