• iOS delegate, 代理/委托与协议.


    之前知知道iOS协议怎么写, 以为真的跟特么java接口一样, 后来发现完全不是.

    首先, 说说应用场景, 就是当你要用一个程序类, 或者说逻辑类, 去控制一个storyboard里面的label, 发现如果直接用

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    //由storyboard根据myView的storyBoardID来获取我们要切换的视图
    mainView= [mainStoryboard instantiateViewControllerWithIdentifier:@"myView"];

    然后去拿这个mainView的label的property, 结果发现这个viewController是可以拿到的, 但是里面的label是nil的.

    结果搜索了一下, 发现逻辑类是无法访问视图控制类的(the real problem is no one told me that after I asked so many people, and it's not a fucking unsual issue, correct?).

    那么好吧, 这个时候, 据说其中一个解决方案就是这个delegate/protocal这个鬼东西.

    首先, 把逻辑类叫做Logic, 视图控制器类就还是ViewController, 在Logic.h里面:


    #import <Foundation/Foundation.h>

    @protocol LogicDelegate <NSObject>
    - (void)DoSomethingOthersCant;
    @end

    @interface Logic : NSObject
    @property (nonatomic, assign) id <LogicDelegate> delegate;
    - (void) changeText;
    @end

    分为两个部分, 一个就是定义协议, 协议名通常是类名+delegate, 在这个例子里面是LogicDelegate, 这部分跟java的接口一个意思, 定义一下抽象方法, 这个例子里面就是DoSomthingOthersCant.

    另一个部分就是定义一个property, 也就是谁实现了这个LogicDelegate接口/协议, 就是他了

    再有一个方法是给逻辑类使用代理方法的.

    那么, Logic.m里面:

    #import "Logic.h"
    #import "ViewController.h"

    @interface Logic ()
    {
    }

    @property (strong,nonatomic) ViewController *viewController;
    @end
    @implementation Logic

    - (void) changeText{
        NSLog(@"xxxx");
       [self.delegate DoSomethingOthersCant];
    }

    @end

    具体的, changeText就在逻辑需要的时候, 去让代理者, 比如视图控制器, 来doSomething它不能do的.

    ok, 到了视图控制器类, 首先要指定这个类, 实现LogicDelegate协议.

    #import <UIKit/UIKit.h>
    #import "Logic.h"

    @interface ViewController : UIViewController<LogicDelegate>  //这儿
    @property (weak, nonatomic) IBOutlet UILabel *label;
    @end

    实现类:

    #import "ViewController.h"
    #import "Logic.h"
    @interface ViewController ()
    @property Logic *logic;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.logic=[[Logic alloc]init];
        self.logic.delegate = self;
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (IBAction)pressed:(id)sender {  
        [self.logic changeText ]; 
    }

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

    - (void)DoSomethingOthersCant
    {
        NSLog(@"dododododo");  
        self.label.text=@"DDDDDDd";
    }

    @end

    首先在viewDidLoad的时候把viewController传到logic的实例的delegate属性.

    self.logic.delegate = self;

    然后定义协议里面要求实现的方法, DoSomethingOthersCant, 内容就是将label的值改变.

    简单来说, 逻辑类定义协议, 协议里面有抽象方法, UIViewController类, 就做代理, 具体定义抽象类的实现, 同时要再viewDidLoad的时候, 逻辑property的delegate要把self传进去.

    多练习吧, 只能这样了.


            

  • 相关阅读:
    Java通过JNI调用C/C++
    Using HTML5 audio and video
    vmstat输出项解释
    uva 11237
    NN优化方法对照:梯度下降、随机梯度下降和批量梯度下降
    认识与学习bash
    系统崩溃,大圣归来
    连载《一个程序员的生命周期》-25.到工业现场学习业务知识引发的思考
    ZOJ问题(2010浙江大学研究生复试上机题目[找规律] hdoj 3788)
    UIView的几个枚举定义
  • 原文地址:https://www.cnblogs.com/Montauk/p/5621806.html
Copyright © 2020-2023  润新知