iOS 개발 - delegate 디자인 모델
delegate 는 결합 을 푸 는 데 사 용 됩 니 다. 대상 에 게 동작 을 간단하게 하지 않 습 니 다.
장면 컨트롤 을 사용 하면 열 시간 이 있 습 니 다. 컨트롤 러 는 이 대리 방법 을 실현 하여 적당 한 시기 에 적당 한 일 을 할 수 있 습 니 다. h 파일
@class TouchView;설정 에이전트
@protocol TouchViewDelegate <NSObject>보기 가 만 졌 을 때 터치 하 는 방법
-(void)touchViewDidTouchBegan:(TouchView *)touchView; @end프 록 시 속성 설정 에이전트 용 assign
@interface TouchView : UIView
@property(nonatomic,assign)id<TouchViewDelegate> delegate;
@end. m 파일 에서 프 록 시 프로 토 콜 을 실행 하 는 방법
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //            [_delegate touchViewDidTouchBegan:self]; }컨트롤 러 에서 협 의 를 준수 하 다
#import "RootViewController.h"
#import "TouchView.h"
@interface RootViewController ()<TouchViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    TouchView *touchView  = [[TouchView alloc]initWithFrame:CGRectMake(20, 100, 275, 100)];
    touchView.backgroundColor = [UIColor redColor];
    [self.view addSubview:touchView];
    [touchView release];
    TouchView *touchView2  = [[TouchView alloc]initWithFrame:CGRectMake(20, 250, 275, 100)];
    touchView2.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:touchView2];
    [touchView2 release];
    TouchView *touchView3  = [[TouchView alloc]initWithFrame:CGRectMake(20, 400, 275, 100)];
    touchView3.backgroundColor = [UIColor blueColor];
    [self.view addSubview:touchView3];
    [touchView3 release];
    //    
    touchView.delegate = self;
    touchView2.delegate = self;
    touchView3.delegate = self;
    //  tag
    touchView.tag = 100;
    touchView2.tag = 101;
    touchView3.tag = 102;
}
//    
-(void)touchViewDidTouchBegan:(TouchView *)touchView
{
    if (touchView.tag == 100) {
        NSLog(@"Color");
        [self changeColor:touchView];
    }else if (touchView.tag == 101)
    {
        NSLog(@"Posist");
        [self changePosist:touchView];
    }else if (touchView.tag ==102)
    {
        NSLog(@"Size");
        [self changeSize:touchView];
    }
}
-(void)changeColor:(TouchView *)touchView
{
    touchView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
-(void)changePosist:(TouchView *)touchView
{
    touchView.center= CGPointMake(arc4random()%(325 - 100 +1)+100, arc4random()%(400-100+1)+100);
}
-(void)changeSize:(TouchView *)touchView
{
    touchView.bounds = CGRectMake(0, 0, arc4random()%(275 - 100 +1)+100, arc4random()%(200-100+1)+100);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
디자인 모델 의 공장 모델, 단일 모델자바 는 23 가지 디자인 모델 (프로 그래 밍 사상/프로 그래 밍 방식) 이 있 습 니 다. 공장 모드 하나의 공장 류 를 만들어 같은 인 터 페 이 스 를 실현 한 일부 종 류 를 인 스 턴 스 로 만 드 는 것...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.