IOS re-talk about proxy transfer value

Source: Internet
Author: User

The example of this post is: There is a and B two interface, to achieve the effect is to let a jump to B, then B has a color parameter, when B jumps to a, the color of the parameter to a, in a to use this color to change the color of their interface. 1th step: In the sender (interface B), the establishment of an agreement (declared in the. h header File)
// 协议名一般以本类的类名开头+Delegate (包含前缀) @protocol ConfigViewControllerDelegate <NSObject>// 声明协议方法,一般以类名开头(不需要前缀)- (void)changeBgColor:(UIColor *)color;

B 是委托 1定义协议, 2声明代理, 3调用协议。
2nd Step: The agent protocol in the. h file in the sender (interface B). Outgoing class--declares the proxy.
@interface ConfigViewController : UIViewController// id即表示谁都可以设置成为我的代理@property (nonatomic,weak) id<ConfigViewControllerDelegate> delegate; // ARC使用weak,MRC使用assign@end
3rd Step: Notify the agent in the method in the sender (interface B) (The most important step)
  //这一步一般是在B跳转到A的方法中实现的,我是在B中创建了一个Button,让这个通知代理在Button中跳转方法中实现。  if ([self.delegate respondsToSelector:@selector(changeBgColor:)]) {     // 加入if语句就是先判断在界面A中是否有changeBgColor这个方法,当有这个方法的时候,才进行代理传值。 //一般会先实例化出一个color的对象,在进行代理传值的时候,是带着这个color一起传递过去的。 [self.delegate changeBgColor:color]; //这里的self是界面B,self.delegate就是界面A了 调用(在第4步和第5步的设置中设置了)}

A 1 Follow protocol 2 to set up proxy 3 to implement protocol incoming class----proxy class4th step: Abide by the protocol in the recipient (interface a).
  @interface ViewController () <ConfigViewControllerDelegate>
5th step: Set yourself as the agent in the recipient (interface a).
  ConfigViewController *testVC = [[ConfigViewController alloc] init];  self;
6th step: Implement the method in the protocol in the recipient (interface a).
  - (void)changeBgColor:(UIColor *)color{  self.view.backgroundColor = color;  }



IOS re-talk about proxy transfer value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.