美文网首页
ios反向传值

ios反向传值

作者: 舒耀 | 来源:发表于2015-04-07 19:32 被阅读2618次

方法三:代码块

根视图代码:

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

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

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

sub1.MyBlock = ^(UIColor * color)

{

self.view.backgroundColor = color;

};

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

}

子视图代码

//.h文件

@interface Sub1ViewController : UIViewController

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

@end

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

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

[self.view addSubview:btn];

}

- (void)btnClick

{

_MyBlock([UIColor orangeColor]);

[self dismissViewControllerAnimated:YES completion:nil];

}

方法四:代理-协议

根视图代码

//.h

#import 

#import "Sub1ViewController.h"

@interface ViewController : UIViewController

@end

//.m

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

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

[self.view addSubview:btn];

}

- (void)btnClick

{

Sub1ViewController * sub1 = [[Sub1ViewController alloc]init];

sub1.view.backgroundColor = [UIColor blueColor];

sub1.delegate = self;

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

}

- (void)changeColor:(UIColor *)color

{

self.view.backgroundColor = color;

}

子视图控制器

//.h文件

#import 

@protocol Sub1ViewControllerDelete 

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

@end

@interface Sub1ViewController : UIViewController

@property (assign,nonatomic,readwrite)id delegate;

@end

//.m文件

- (void)viewDidLoad

{

[super viewDidLoad];

[self createPopToRootViewBtn];

}

- (void)createPopToRootViewBtn

{

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(10, 30, 300, 40);

[btn setTitle:@"进入根视图控制器"forState:UIControlStateNormal];

btn.layer.cornerRadius = 5;

btn.backgroundColor = [UIColor blackColor];

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

[self.view addSubview:btn];

}

- (void)btnClick

{

[_delegate changeColor:[UIColor orangeColor]];

[self dismissViewControllerAnimated:YES completion:nil];

}

方法一:通知中心

发布通知消息postNotificationName消息的名字userInfo传的参数可有可无

NSDictionary*dic = [[NSDictionaryalloc]initWithObjectsAndKeys:numStr,@"messageNumber",nil];

[[NSNotificationCenterdefaultCenter]postNotificationName:@"message"object:nil userInfo:dic];

接收通知中心消息  @selector(myMessage:)调用的方法

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(myMessage:)name:@"message"object:nil];

- (void)registUserName:(NSNotification*)userName

{

NSLog(@"username%@",[userName.userInfoobjectForKey:@"userName"]);

loginLabel.text= [userName.userInfoobjectForKey:@"userName"];

}

相关文章

  • 【iOS开发细节】之- delegate代理的使用

    在iOS开发中、好多时候需要涉及到页面传值、而传值又分为正向传值和反向传值 一、 传值 1、正向传值 2、反向传值...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS反向传值整理

    本文主要讲解iOS中常见的反向传值方法。 1.AppDelegate传值 在AppDelegate中定义相关的属性...

  • iOS (反向)传值

    代理、block、消息中心、单例。 正向传值 通过属性(特性)的值,在上个使用本类(所在类)对象的类中,直接传递其...

  • ios反向传值

    方法三:代码块 根视图代码: //.m文件 -(void)viewDidLoad { [superviewDidL...

  • iOS反向传值

    常用的方式 代理 block 通知 也可以使用以下方式但不推荐 单例 全局变量 NSUserDefaults 文件...

  • iOS中传值问题

    1.在iOS中正向传值 可以用属性就不多说了,反向传值 从image中传值给controller的时候 我们可以用...

  • iOS回调,代理和block

    iOS中我们经常会遇到正反向传值,正向传值就不用多介绍了,就是属性传值(当然也可以有其他方式,这里不做介绍了). ...

  • OC中反向传值的方法

    oc中反向传值四种方法 block反向传值 在需要传值的界面: 在接受到传值的界面 单例反向传值 创建一个单例类 ...

网友评论

      本文标题:ios反向传值

      本文链接:https://www.haomeiwen.com/subject/rgtoxttx.html