//大体意思为我们在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(100, 100, 60, 60);
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];
}
#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(0, 100, 60, 60);
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(0, 200, 320, 30)];
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
let rvc:RootViewController = RootViewController()
let nav=UINavigationController(rootViewController: rvc)
self.window?.rootViewController=nav
//不做解释
//
// 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(.System) as 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(100) as 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(.System) as 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的代码 转载请注明,谢谢