iOS ViewController之间传值方法三(利用NSNotification)
来源:互联网 发布:人工智能出现表明 编辑:程序博客网 时间:2024/06/07 09:39
//注册通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(click:)name:@"mynotification"object:nil];
UIButton *jumpBtn = [[UIButtonalloc]initWithFrame:CGRectMake(50,100,200, 50)];
jumpBtn.backgroundColor = [UIColorredColor];
[jumpBtn setTitle:@"click..."forState:UIControlStateNormal];
[jumpBtn addTarget:selfaction:@selector(clickBtn)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:jumpBtn];
------------------------------------------------------
-(void)click:(NSNotification*)text{
NSString *str = text.userInfo[@"1"];
NSLog(@"---------------%@",str);
}
-(void)clickBtn{
//PS:UIViewController 跳转方法有两种,1.利用UIViewController自身的presentModalViewController,进行跳转;调用dismissModalViewControllerAnimated方法可以返回。2.利用UINavigationController,调用pushViewController,进行跳转;这种采用压栈和出栈的方式,进行Controller的管理。调用popViewControllerAnimated方法可以返回.
TwoViewController *vc = [[TwoViewControlleralloc]init];
[selfpresentViewController:vcanimated:YEScompletion:nil];
// Or [self.navigationController pushViewController:vc animated:YES];
}
NSDictionary *dic = [[NSDictionaryalloc]initWithObjectsAndKeys:@"this",@"1",@"is",@"2",@"a",@"3",@"dictionary",@"4",nil];
//创建通知
NSNotification *myNotification = [NSNotificationnotificationWithName:@"mynotification"object:niluserInfo:dic];
//通过通知中心发送通知
[[NSNotificationCenterdefaultCenter]postNotification:myNotification];
移除通知:removeObserver:和removeObserver:name:object:
其中,removeObserver:是删除通知中心保存的调度表一个观察者的所有入口,而removeObserver:name:object:是删除匹配了通知中心保存的调度表中观察者的一个入口。
这个比较简单,直接调用该方法就行。例如:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意参数notificationObserver为要删除的观察者,一定不能置为nil。
- iOS ViewController之间传值方法三(利用NSNotification)
- iOS ViewController之间传值方法一(利用临时变量)
- iOS ViewController之间传值方法二(利用NSUserDefaults)
- iOS ViewController之间传值方法四(利用Delegate)
- iOS ViewController之间传值方法五(使用block)
- iOS利用通知(NSNotification)进行传值
- iOS利用通知(NSNotification)进行传值
- iOS两个viewController之间传值
- IOS 两个ViewController 之间传值
- ViewController之间传值
- 【ios】2个viewController之间传值(segue方式)
- iOS 属性传值 Block传值 两个ViewController之间的
- IOS学习之——ViewController之间正向传值
- iOS 不同界面(ViewController)之间传值
- iOS NSNotification 通知传值
- iOS NSNotification 通知传值
- iOS NSNotification 通知传值
- Objective-C ViewController之间传值的方法
- 黑马程序员-面向对象
- KVC和KVO
- ubuntu12.04搭建gitlab
- 隐藏导航条底部的横线
- hibernate中的二级缓存是如何工作的
- iOS ViewController之间传值方法三(利用NSNotification)
- 3. Spring Boot 1.2.5使用redis做数据缓存
- [Kali Linux 2.0] DSL配置问题
- Python读取配置文件
- 4. Spring Boot 1.2.5,Spring Data JPA多数据源支持
- POJ 2485-Highways(并查集)
- 读《第一行代码——安卓》2.3.3
- IO多路复用的几种实现机制的分析
- 编码