swift中的反向代理传值与Object-C中的对比

16 篇文章 0 订阅
8 篇文章 0 订阅

//大体意思为我们在sub类中声明一个协议,协议中有2个方法,然后Root类遵守协议,并代理实现这两个方法,然后再sub中进行反向传值给Root类实现Root类的改变


我们先讲OC中的 当然是首先建立一个空白工程,和两个类,一个是RootViewController和SubViewController()

在Appdelegate中写

  RootViewController *rvc=[[RootViewController alloc]init];

    self.window.rootViewController=rvc;

    self.window.backgroundColor = [UIColor whiteColor];

 在SubviewController中.h文件中

//这里是先建立了一个协议,协议中有2个可选的方法

@protocol changeDelegate <NSObject>

@optional

//改变颜色

-(void)changeColor:(UIColor *)color;

//改变值

-(void)changeValue:(NSString*)str;


@end


@interface SubViewController : UIViewController

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

@end


.m文件中

- (void)viewDidLoad

{

    [super viewDidLoad];


    [self createUI];

}

-(void)createUI

{

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

    button.frame=CGRectMake(1001006060);

    button.backgroundColor=[UIColor redColor];

    [button setTitle:@"Sub" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];


}

-(void)buttonClick

{

  //这里是向子界面向主界面传值

    if ([self.delegate respondsToSelector:@selector(changeColor:)]) {

        [self.delegate changeColor:[UIColor blueColor]];

    }

    if ([self.delegate respondsToSelector:@selector(changeValue:)]) {

        [self.delegate changeValue:@"是的,人都是会变的"];

    }

  

    [self dismissViewControllerAnimated:YES completion:nil];


}


RootViewController的.h文件中不添加任何东西,.m文件中


#import "RootViewController.h"

#import "SubViewController.h"

@interface RootViewController ()<changeDelegate>//这里表示要遵守改协议


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

[self createUI];

}

-(void)createUI

{

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

    button.frame=CGRectMake(01006060);

    button.backgroundColor=[UIColor redColor];

    [button setTitle:@"Root" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(020032030)];

      label.text=@"你说我会变吗";

    label.tag=100;

    label.textColor=[UIColor greenColor];

    label.textAlignment=NSTextAlignmentCenter;

    [self.view addSubview:label];


    


}


-(void)buttonClick

{

   SubViewController *sub=[[SubViewController alloc]init];

    sub.delegate=self//表示代理,由Root代理sub,Root实现方法

    [self presentViewController:sub animated:YES completion:nil];



}

//实现协议中的两个方法

-(void)changeColor:(UIColor *)color

{

    self.view.backgroundColor=color;

    

}

-(void)changeValue:(NSString *)str

{

    UILabel *label=(UILabel *)[self.view viewWithTag:100];

    label.text=str;

    

    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


下面我们将swift代理传值的实现 仍然是建立空白工程(空白工程的建立先前已经提到)和两个类rootViewController和SubViewController

在appdelegate中需要这样写

 let rvc:RootViewController = RootViewController()

        let nav=UINavigationController(rootViewController: rvc)

        self.window?.rootViewController=nav

//不做解释
RootViewController中

//

//  RootViewController.swift

//  Swift反向代理传值

//

//  Created by ytdxxt on 14-12-17.

//  Copyright (c) 2014 xutao. All rights reserved.

//


import Foundation

import UIKit

class RootViewController: UIViewController,changeDelegate {

    

    override func viewDidLoad() {

        

       //建立一个button

        var but=UIButton.buttonWithType(.Systemas UIButton

        var frame=CGRect(x: 100, y: 100, width: 100, height: 44)

        but.frame=frame

        but.setTitle("Click", forState: .Normal)

        but.addTarget(self, action: "Click:", forControlEvents: .TouchUpInside)

       self.view.addSubview(but)

        //建立一个label

        var label=UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 44)) asUILabel

        label.text="你说我会变吗?"

       label.textColor=UIColor.blackColor()

        label.tag=100

        self.view.addSubview(label)

        

        

    }

    //实现代理方法 设定颜色

     func changeColor(controller: UIViewController, color: UIColor) {

        println("color\(color)")

        self.view.backgroundColor=color

    }

    //实现第二个代理方法,实现label内容的变化

    func changeLableText(controller: UIViewController, labelText: NSString) {

        var label=self.view.viewWithTag(100as UILabel

        label.text=labelText

        

    }

    

    func Click(sender:UIButton){

        let svc:SubViewController=SubViewController()

             svc.delegate=self

        self.navigationController?.pushViewController(svc, animated: true)

//        self.presentViewController(svc, animated: true, completion: nil)

    

    }

}

SubViewController中

//

//  SubViewController.swift

//  Swift反向代理传值

//

//  Created by ytdxxt on 14-12-17.

//  Copyright (c) 2014 xutao. All rights reserved.

//


import Foundation

import UIKit

//建立一个协议,里面有两个方法,必须实现

protocol changeDelegate : NSObjectProtocol{


    func changeColor(controller:UIViewController,color:UIColor)

    func changeLableText(controller:UIViewController,labelText:NSString

)


}

class SubViewController: UIViewController {

    var delegate:changeDelegate?

    override func viewDidLoad() {

        //创建一个button

        

        var backButton=UIButton.buttonWithType(.Systemas UIButton

        var frame=CGRect(x: 100, y: 100, width: 100, height: 44)

        backButton.frame=frame

        backButton.setTitle("backButton", forState: .Normal)

        backButton.addTarget(self, action: "backButtonClick:", forControlEvents: .TouchUpInside)

        self.view.addSubview(backButton)

        

        

    }

    func backButtonClick(sender:UIButton){

        

      

        let rvc:RootViewController=RootViewController()

       if((delegate) != nil){

        //这里表示子界面向主界面传值

        self.delegate?.changeColor(rvc, color: .redColor())

        self.delegate?.changeLableText(rvc, labelText: "是的,人都是会变的")

      self.navigationController!.popToRootViewControllerAnimated(true)

//        self .dismissViewControllerAnimated(true, completion: nil)

        }

    

    }

}

好了,这样就都做完了,由于Swift现在正在学习阶段,写的不好请大神见谅,不喜可喷,后续将继续更新更多关于Swift的代码 转载请注明,谢谢

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值