iOS利用通知逆传值
直接创建两个控制器,点击跳转第二个界面,然后点击按钮进行传值
#import "ViewController.h"
#import "TWOOViewController.h"
@interface ViewController ()
@property(nonatomic,weak)UILabel * label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 50)];
nextBtn.backgroundColor = [UIColor redColor];
[nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[nextBtn setTitle:@"下一个界面" forState:UIControlStateNormal];
[self.view addSubview:nextBtn];
UILabel * labeltext = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, 50)];
labeltext.backgroundColor = [UIColor yellowColor];
labeltext.textColor = [UIColor greenColor];
[self.view addSubview:labeltext];
self.label = labeltext;
//创建通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nexttow:) name:@"nextBtnaa" object:nil];
}
-(void)nextBtnClick:(UIButton*)nextBtn{
TWOOViewController * two = [[TWOOViewController alloc]init];
[self presentViewController:two animated:YES completion:nil];
}
-(void)nexttow:(NSNotification*)nontification{
self.label.text = nontification.object;
}
第二个控制器
#import "TWOOViewController.h"
@interface TWOOViewController ()
@end
@implementation TWOOViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 79, self.view.bounds.size.width, 50)];
[backBtn setTitle:@"回" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
}
-(void)backBtnClick:(UIButton*)backBtn{
[[NSNotificationCenter defaultCenter]postNotificationName:@"nextBtnaa" object:@"逆传值"];
//隐藏本控制器
[self dismissViewControllerAnimated:YES completion:nil];
}
结果:
最新文章
- 一个简单的MVC实例及故障排除
- [非原创]eclipse中一些常见svn图标的含义
- [C#基础知识] ReadOnly关键字修饰的变量可以修改,只是不能重新分配
- EF架构~数据分批批量提交
- Elasticsearch聚合初探——metric篇
- TCP/IP 协议介绍
- 关于Hibernate中的Configuration
- 关于java对象的思考
- Gridview中修改某列的背景色
- CentOS6.7 下安装git
- iOS PureLayout使用
- Qt之添加图标
- Google Maps API的使用
- BZOJ 2594 水管局长数据加强版
- 【NET Core】Nuget包发布流程
- linux中gdb的使用
- Elastic Stack之ElasticSearch分布式集群二进制方式部署
- 使用Nexus配置Maven私有仓库
- (转)深度学习目标检测指标mAP
- CCF CSP认证考试试题