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

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

iOS頁面間傳值之屬性傳值,代理傳值

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

手機 APP 運行,不同頁面間傳值是必不可少,傳值的方式有很多(方法傳值,屬性傳值,代理傳值,單例傳值) ,這里主要總結下屬性傳值和代理傳值.

屬性傳值:屬性傳值是最簡單,也是最常見的一種傳值方式,但其具有局限性(一般用于將第一個頁面的值傳遞到第二個頁面,但無法從第二個頁面傳到第一個頁面),

  向SecondViewController傳值:SecondViewController 設置屬性 sendMessage

1 - (void)rightButtonAction:(UIBarButtonItem *)sender{2     SecondViewController *secondVC = [[SecondViewController alloc]init];3     secondVC.sendMessage = self.rootView.textField.text;4     [self.navigationController pushViewController:secondVC animated:YES];5 }

 

代理傳值:較難,不易理解,通常用于在第二個頁面向第一個頁面傳值.一般分為六步

(例子采用 navigationController 跳轉頁面)

1.聲明協議 (寫在第二個頁面)

@PRotocol myDelegete <NSObject>- (void)sendMessage:(NSString*)message;@end

 

2.定義遵守協議的屬性 (寫在第二個頁面) (屬性必須用 assign )

@property (nonatomic , assign)id<myDelegete> delegate;

 

3.遵守協議(寫在第一個頁面)

1 @interface RootViewController : UIViewController <myDelegete>

 

4.設置代理 (設置代理寫在跳轉事件內) (寫在第一個頁面)

 

1 - (void)rightButtonAction:(UIBarButtonItem *)sender{2     SecondViewController *secondVC = [[SecondViewController alloc]init];3     secondVC.sendMessage = self.rootView.textField.text;4     [self.navigationController pushViewController:secondVC animated:YES];5     //代理傳值第四步6     secondVC.delegate = self;7     8 }

 

 

 

5.實現協議方法 (寫在第一個頁面)

 

1 - (void)sendMessage:(NSString *)message{2     self.rootView.textField.text = message;3 }

 

 

 

6.實現傳值 (寫在第二個頁面)

1 - (void)leftButtonAction:(UIBarButtonItem *)sender{2     [self.navigationController popViewControllerAnimated:YES];3     //代理傳值第六步:4     [self.delegate sendMessage:self.secondView.textField.text];5 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸溪县| 康平县| 涞源县| 昌吉市| 交城县| 正阳县| 赣州市| 海晏县| 古田县| 永顺县| 临澧县| 黎平县| 米林县| 玉田县| 大化| 宽城| 广平县| 南雄市| 中牟县| 舟山市| 洮南市| 日喀则市| 吉安县| 成都市| 阜南县| 商南县| 新巴尔虎左旗| 富裕县| 南华县| 濮阳县| 丹棱县| 图们市| 广州市| 大邑县| 柳州市| 陇南市| 灵山县| 潮安县| 鄱阳县| 波密县| 邳州市|