国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS設置代理過程

2019-11-14 18:05:21
字體:
來源:轉載
供稿:網友

iOS設置代理的過程 (以模擬 button 作用為例)

1.寫協議

  新建一個名為 MyButton 的文件,繼承于 UIView,在該文件里 聲明協議 myDelegate

2.寫協議方法

  為聲明的協議添加方法 

3.定義一個遵守協議的屬性

  前三步代碼如下:

 1 #import <UIKit/UIKit.h> 2 @class MyButton; 3 //第一步:寫協議 4 @PRotocol myDelegate <NSObject> 5 //第二步:寫協議方法 (參數要在上面聲明:@class MyButton;) 6 - (void)changeColor:(MyButton *) myButton; 7 @end 8  9 @interface MyButton : UIView10 //第三步:寫一個myDelegate屬性11 @property (nonatomic , assign)id<myDelegate>  delegate;12 13 @end

 

4.寫一個調用協議的方法

  寫touchesBegan 方法

5.調用協議方法

  4,5步代碼:

1 //第四步 寫touchesBegan 方法2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{3     //第五步調用協議的方法4     [_delegate changeColor:self];5 }

 

6.遵守協議

  讓要實現協議的類遵守協議

 1 //第六步 遵循協議 2 @interface RootViewController : UIViewController <myDelegate> 

 

7.設置代理

  self.rootView.myButton.delegate = self; 

8.實現協議方法

1 //第八步 實現協議中的方法2 - (void)changeColor:(MyButton *)myButton{3     myButton.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];4 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北票市| 磐石市| 肃宁县| 安丘市| 渭南市| 宣汉县| 柳江县| 五河县| 稷山县| 浦江县| 禄劝| 临澧县| 威宁| 清徐县| 鄱阳县| 大兴区| 尼木县| 合作市| 晴隆县| 天水市| 鄂托克旗| 沂源县| 宜良县| 开平市| 松溪县| 越西县| 高密市| 南澳县| 宣武区| 九寨沟县| 鹤峰县| 集贤县| 鄢陵县| 嘉黎县| 瓮安县| 雷波县| 乌兰察布市| 南阳市| 永寿县| 宜阳县| 永年县|