zoukankan      html  css  js  c++  java
  • ObjectiveC Protocol使用三步走

          protocol应用两个controller之间或者controller与自定义UIView之间的相互调用。

          它的应用类似java或者C#里的interface。假设A调用B,B回调A,那么就可以如此设计:

          首先:定义一个protocal:

     @protocol PtlChart


    @optional
    -(void) callBack:(id)arg;

    @required
    -(void) loadData;
    @end

          第二步:在B里定义protocal的delegate:

    代码
    //B.h
    @interface B : UIView {
         id
    <PtlChart>      _delegate;
    }

    @property (nonatomic,retain)       id
    <PtlChart>  delegate;

    @end

    //B.m:
    #import "B.h"


    @implementation B

    @synthesize 
    delegate = _delegate;

    - (void)dealloc {
         [_delegate release];

    @end

          第三步:在A里调用B时,把delegate传递过去:

    代码
    //A.h:
    @interface A : UIViewController<PtlChart> 

    @end
    //A.m:
    #import "A.h"

    @implementation A

    - (void) loadData {
         A 
    *= [[A alloc] init];
        [a setDelegate:self];   
    //<<--------- here
       [self.view addSubview:a];
       [a release];
    }

    -(void) callBack:(id)arg{
        
    int i = (int)arg;
        NSLog(
    @"callback= %d",i);
    }
    @end

         

  • 相关阅读:
    前端教程-3.调试工具(上)
    常用跨域方法实践(二)
    常用跨域方法实践(一)
    移动web问题小结
    Promise实现原理
    H5页面的跨webview预拉取数据尝试
    【译】编写高性能JavaScript
    校招社招必备核心前端面试问题与详细解答
    高性能极致用户体验前端开发实战
    http请求代理proxy-ajax
  • 原文地址:https://www.cnblogs.com/KiloNet/p/1825016.html
Copyright © 2011-2022 走看看