zoukankan      html  css  js  c++  java
  • UI 多个视图控制器(UIViewController)间的 协议传值

      1 #import <UIKit/UIKit.h>
      2 
      3 
      4 
      5 //1.协议传值
      6 
      7 // 协议由后面的视图控制器制定
      8 
      9 @protocol secondDelegate <NSObject>
     10 
     11 
     12 
     13 //协议的方法需要带一个或多个参数
     14 
     15 - (void)passValueWithString:(NSString *)string;
     16 
     17 
     18 
     19 @end
     20 
     21 
     22 
     23 @interface secondViewController : UIViewController
     24 
     25 
     26 
     27 //2.设置自己的 代理人 属性
     28 
     29 @property (nonatomic, assign) id<secondDelegate> delegate;
     30 
     31 
     32 
     33 @end
     34 
     35 ==================================================
     36 
     37 - (void)buttonAction:(UIButton *)button
     38 
     39 {
     40 
     41     NSLog(@"suck");
     42 
     43     //3.让自己的代理人 调用 协议方法
     44 
     45     [self.delegate passValueWithString:button.currentTitle];
     46 
     47     [self.navigationController popViewControllerAnimated:YES];
     48 
     49 }
     50 
     51 ==================================================
     52 
     53 在第二个视图控制器的 .h 文件的具体方法中  让自己的代理人 调用 协议方法
     54 
     55 - (void)buttonAction:(UIButton *)button
     56 
     57 {
     58 
     59     NSLog(@"suck");
     60 
     61     //3.让自己的代理人 调用 协议方法
     62 
     63     [self.delegate passValueWithString:button.currentTitle];
     64 
     65     [self.navigationController popViewControllerAnimated:YES];
     66 
     67 }
     68 
     69 
     70 ==================================================
     71 
     72 #import <UIKit/UIKit.h>
     73 
     74 #import "secondViewController.h"
     75 
     76 //4.由第一个viewController 签订 第二个viewController的协议
     77 
     78 @interface mainViewController : UIViewController <secondDelegate>
     79 
     80 
     81 
     82 @end
     83 
     84 
     85 
     86 ===================================================
     87 
     88 在第一个视图控制器的 .h 文件里的具体方法中实现 以下:
     89 
     90 
     91 
     92 - (void)buttonAction:(UIButton *)button
     93 {
     94     secondViewController *secondVC = [[secondViewController alloc]init];
     95     
     96     //5.给第二个viewController 指定代理人
     97     [secondVC setDelegate:self];
     98     
     99     [self.navigationController pushViewController:secondVCanimated:YES];
    100     [secondVC release];
    101 }
    102 
    103 //6.实现协议的方法
    104 - (void)passValueWithString:(NSString *)string
    105 {
    106     NSLog(@"从第二个viewController传来的值: %@", string);
    107     UILabel *label = (UILabel *)[self.view viewWithTag:50];
    108     [label setText:string];
    109 }
    有人说:爱上一座城,是因为城里住着某个人,能够与所爱的人在一起,连光阴都是美的。即便粗茶淡饭,修篱种田,只要有你陪伴就好。那么,找一个青山绿水的地方,寻一处幽静的茅舍,或是云水禅心的庭院,那里有晴朗的阳光和静谧的悠然,还有你明媚的笑脸。掬一捧花香在平淡的日子,握着一路相随的暖意,让爱的馨香在柴米油盐中升腾;在一杯茶的温情里,体味生活的诗意;在一碗粥的清淡中,感受生活的浪漫,每天清晨你和阳光都在,便是我的幸福。——春暖花开 《择一城终老,遇一人白首》
  • 相关阅读:
    idea中git分支的使用
    常用的分布式事务解决方案
    分布式事务解决方案总结
    IDEA中Git的更新、提交、还原方法
    (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库
    Git 安装及用法 github 代码发布 gitlab私有仓库的搭建
    主机ping不通虚拟机,但是虚拟机能ping通主机
    Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64
    windows
    mingw-w64线程模型:posix vs win32(posix允许使用c++11的std:: thread,但要带一个winpthreads,可能需要额外dll)
  • 原文地址:https://www.cnblogs.com/-Eric-Liu/p/5563958.html
Copyright © 2011-2022 走看看