iOS delegate, 代理/委託與協議.

來源:互聯網
上載者:User

標籤:

之前知知道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");  
    [email protected]"DDDDDDd";
}

@end

首先在viewDidLoad的時候把viewController傳到logic的執行個體的delegate屬性.

self.logic.delegate = self;

然後定義協議裡面要求實現的方法, DoSomethingOthersCant, 內容就是將label的值改變.

 

簡單來說, 邏輯類定義協議, 協議裡面有抽象方法, UIViewController類, 就做代理, 具體定義抽象類別的實現, 同時要再viewDidLoad的時候, 邏輯property的delegate要把self傳進去.

 

多練習吧, 只能這樣了.

 


        

iOS delegate, 代理/委託與協議.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.