UIViewController之间传值:

定义两个UIViewController:GCVC1和GCVC2,GCVC1作为根视图,

然后通过GCVC1把GCVC2显示出来,然后在GCVC2视图切换到GCVC1视图的同时传递给GCVC1数据改变GCVC1的背景色为紫色(purpleColor)。

这个跟导航控制器中两个相邻视图控制器之间的传值是通用的。

一下提供几种可行的方法,

1.代理:

/**************GCVC2*****************/

协议声明:

@protocol SendMessage <NSObject>


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


@end


申明GCVC2:


#import <UIKit/UIKit.h>


@interface GCVC2 : UIViewController<SendMessage>


@property(nonatomic,assign)id delegate;

@end


#import "GCVC2.h"

#import "GCVC1.h"

#import "GCTools.h"


实现GCVC2:

@interface GCVC2 ()

@end

@implementation GCVC2

@synthesize delegate;

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 1" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor blueColor] anPointofX:50 anPointofY:200 andWidth:100andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

}

-(void)btnClick{

    /**

     *  代理实现的类:在VC1btnClick()函数中我们让VC2.delegate = self,所以可以调用下面的这个方法:

     */

    [delegate sendMessage:[UIColor purpleColor]];

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end



/**************GCVC1*****************/

声明GCVC1:

#import "GCVC2.h"

#import <UIKit/UIKit.h>


@interface GCVC1 : UIViewController<SendMessage>


@end

#import "GCVC1.h"

#import "GCTools.h"

#import "GCVC2.h"

@interface GCVC1 ()


@end

实现GCVC1:

@implementation GCVC1


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

   //创建按钮,这个GCTools是我自己写的类,为了创建按钮方便。

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 2" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor redColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

//按钮监听事件:

-(void)btnClick{

    GCVC2 *VC2 = [[GCVC2 alloc] init];

    VC2.delegate = self;

   //加载第二个视图控制器

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

}

//实现协议的方法:

-(void)sendMessage:(UIColor *)color{

    self.view.backgroundColor = color;

}

@end



点语法:

/**************GCVC2*****************/

声明GCVC2:

#import <UIKit/UIKit.h>


@interface GCVC2 : UIViewController

//定义两个私有变量:

@property(nonatomic,assign)id delegate;

@property(nonatomic,assign)SEL action;

@end

实现GCVC2:

#import "GCVC2.h"

#import "GCVC1.h"

#import "GCTools.h"



@interface GCVC2 ()

@end

@implementation GCVC2

@synthesize delegate;

@synthesize action;

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 1" andUIButtonType:UIButtonTypeCustomandControlState:UIControlStateNormal andBackgroundColor:[UIColor blueColor] anPointofX:50 anPointofY:200 andWidth:100andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

}

-(void)btnClick{

    [delegate performSelector:action withObject:[UIColor purpleColor]];

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end


/**************GCVC1*****************/

声明GCVC1:

#import "GCVC2.h"

#import <UIKit/UIKit.h>


@interface GCVC1 : UIViewController


@end


实现GCVC1:

#import "GCVC1.h"

#import "GCTools.h"

#import "GCVC2.h"

@interface GCVC1 ()


@end


@implementation GCVC1

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

   //创建按钮,这个GCTools是我自己写的类,为了创建按钮方便。

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 2" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor redColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

//按钮监听事件:

-(void)btnClick{

    GCVC2 *VC2 = [[GCVC2 alloc] init];

    VC2.delegate = self;

    VC2.action = @selector(sendMessage:);

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

}

//实现的方法:

-(void)sendMessage:(UIColor *)color{

    self.view.backgroundColor = color;

}

@end

通知:

/**************GCVC2*****************/

声明GCVC2:

#import <UIKit/UIKit.h>


@interface GCVC2 : UIViewController

@end

实现GCVC2:

#import "GCVC2.h"

#import "GCVC1.h"

#import "GCTools.h"



@interface GCVC2 ()

@end

@implementation GCVC2

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 1" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormalandBackgroundColor:[UIColor blueColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    

    //注册系统默认通知中心

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    //创建广播消息

    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor purpleColor] forKey:@"color"];

    [nc postNotificationName:@"GCVC2" object:self userInfo:dic];

}

-(void)btnClick{

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end

/**************GCVC1*****************/

声明GCVC1:

#import "GCVC2.h"

#import <UIKit/UIKit.h>


@interface GCVC1 : UIViewController


@end

实现GCVC1:

#import "GCVC1.h"

#import "GCTools.h"

#import "GCVC2.h"

@interface GCVC1 ()


@end


@implementation GCVC1


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    //创建按钮,这个GCTools是我自己写的类,为了创建按钮方便。

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 2" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor redColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    //创建广播监听器

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    //第一个参数:观察者对象

    //第二个参数:观察者收到通知后调用的方法,

    //第三个参数:第三个参数是观察的对象的名字(电台频率)

    //第四个参数:指定接收那个对象发送来得广播信息。

    [nc addObserver:self selector:@selector(sendMessage:) name:@"GCVC2" object:nil ];

}


//按钮监听事件:

-(void)btnClick{

    GCVC2 *VC2 = [[GCVC2 alloc] init];

    //加载第二个视图控制器

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

}


//实现广播事件的监听方法:

-(void)sendMessage:(NSNotification *)nc{

    NSDictionary *dic = nc.userInfo;

    self.view.backgroundColor = [dic objectForKey:@"color"];

}

@end


还有一种·······coming·······
Block:块

/**************GCVC2*****************/

声明GCVC2:


#import <UIKit/UIKit.h>


@interface GCVC2 : UIViewController

@property(nonatomic,copy)void (^Block)(UIColor * color);


@end



实现GCVC2:

#import "GCVC2.h"

#import "GCVC1.h"

#import "GCTools.h"



@interface GCVC2 ()

@end

@implementation GCVC2

@synthesize Block;

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 1" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor blueColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    

}

-(void)btnClick{

    Block([UIColor purpleColor]);

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end



/**************GCVC1*****************/

声明GCVC1:

#import "GCVC2.h"

#import <UIKit/UIKit.h>

typedef void (^MyBlock)();

@interface GCVC1 : UIViewController

@property(nonatomic,copy)void (^Block)(UIColor *color);

@property(nonatomic,copy)MyBlock MB;

@end



实现GCVC1:

#import "GCVC1.h"

#import "GCTools.h"

#import "GCVC2.h"


@interface GCVC1 ()

@end

@implementation GCVC1

@synthesize Block;

@synthesize MB;

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    //创建按钮,这个GCTools是我自己写的类,为了创建按钮方便。

    UIButton *btn = [GCTools ButtonViewWithTitle:@"TO 2" andUIButtonType:UIButtonTypeCustom andControlState:UIControlStateNormal andBackgroundColor:[UIColor redColor] anPointofX:50 anPointofY:200 andWidth:100 andHeight:50 andSuperView:nil andTag:1];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

}


//按钮监听事件:

-(void)btnClick{

    GCVC2 *VC2 = [[GCVC2 alloc] init];

    //加载第二个视图控制器

    GCVC1 *VC1 = [[GCVC1 alloc] init];

    VC1.Block = ^(UIColor *color){

        self.view.backgroundColor = color;

    };

    VC2.Block = VC1.Block;

    [self presentViewController:VC2 animated:YES completion:^{

        NSLog(@"Block Start");

    }];

}


//实现广播事件的监听方法:

-(void)sendMessage:(NSNotification *)nc{



}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值