日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oc中代理的简单运用

發(fā)布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oc中代理的简单运用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天和以為老同學(xué)聊了一些,深有感觸,看它傳值都是用代理寫的,自己平時都是用block,通知之類的,基本不用代理,想想代理,感覺自己也有些模棱兩可,所以動手寫了一個代理簡單運(yùn)用的demo,寫完之后思考了一番,在與block和通知做一些比較,豁然開朗,感覺自己在以后又多了一大助力。

我一貫的態(tài)度,做項(xiàng)目的思路源于基礎(chǔ),我不奢求自己什么都會,只要求自己學(xué)的都能基本掌握原理,從而達(dá)到心到,手到,運(yùn)用自如。

關(guān)于代理之類的官方解釋,我在此就不說了,不懂得,自己去查閱一些文檔吧,這里只寫它的原理和用法。

? ?代理不是類,只是一個方法聲明的文件,先看它的創(chuàng)建方法

?

file:///Users/mac/Desktop/01.png

file:///Users/mac/Desktop/02.png

代理文件中

// // ViewDelegate.h // 代理的簡單應(yīng)用 // // Created by mac on 16/3/9. // Copyright ? 2016年 WYP. All rights reserved. //#import <Foundation/Foundation.h>@protocol ViewDelegate <NSObject>- (void)creatViewInView;- (void)addSub:(NSString *)str;- (int)upSomething:(CGFloat)x;@end

?被委托方,簽署代理,實(shí)現(xiàn)代理方法的一方

// // ViewController.m // 代理的簡單應(yīng)用 // // Created by mac on 16/3/9. // Copyright ? 2016年 WYP. All rights reserved. //#import "ViewController.h" #import "SViewController.h" @interface ViewController ()<ViewDelegate>//類簽署協(xié)議@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];SViewController *view = [[SViewController alloc] init]; #warning 設(shè)置代理對象view.ViewDelegate = self;} #pragma mark ViewDelegate - (int)upSomething:(CGFloat)x{NSLog(@"x:%.1f",x);return 15; } - (void)addSub:(NSString *)str{NSLog(@"str:%@",str); } - (void)creatViewInView{NSLog(@"創(chuàng)建了"); }@end

?委托方,調(diào)用代理方法的一方

// // SViewController.h // 代理的簡單應(yīng)用 // // Created by mac on 16/3/9. // Copyright ? 2016年 WYP. All rights reserved. //#import <UIKit/UIKit.h> #import "ViewDelegate.h"//引入?yún)f(xié)議 @interface SViewController : UIViewController //設(shè)置協(xié)議屬性,用于決定代理方法什么時候執(zhí)行 @property (nonatomic, weak) id<ViewDelegate> ViewDelegate;@end

?

// // SViewController.m // 代理的簡單應(yīng)用 // // Created by mac on 16/3/9. // Copyright ? 2016年 WYP. All rights reserved. //#import "SViewController.h"@interface SViewController ()@end@implementation SViewController- (id)init{if (self = [super init]) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 判斷如果代理對象不存在,不執(zhí)行代理方法if (!_ViewDelegate) {return ;} #pragma mark 調(diào)用代理中的方法 // 無參數(shù)也無返回值的代理方法if ([_ViewDelegate respondsToSelector:@selector(creatViewInView)]) {//外部判斷,如果代理對象響應(yīng)這個代理方法,才讓其執(zhí)行這個代理方法[_ViewDelegate creatViewInView];}// 有參數(shù)無返回值的代理方法if ([_ViewDelegate respondsToSelector:@selector(addObject:)]) {[_ViewDelegate addSub:@"zhang"];}// 有參數(shù)有返回值得代理方法if ([_ViewDelegate respondsToSelector:@selector(upSomething:)]) {int i = [_ViewDelegate upSomething:34.5];NSLog(@"i=%d",i);}});}return self; }- (void)viewDidLoad {[super viewDidLoad];} - (void)setViewDelegate:(id<ViewDelegate>)ViewDelegate{_ViewDelegate = ViewDelegate;}@end

?

轉(zhuǎn)載于:https://www.cnblogs.com/zxh-iOS/p/5260190.html

總結(jié)

以上是生活随笔為你收集整理的oc中代理的简单运用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。