iOS 传值
2015-10-15 15:21
435 查看
首页页面
下一页页面
传值后首页页面
下一页页面
传值后首页页面
通知传值
- (void)viewDidLoad { [super viewDidLoad]; // 注册监听者 // name 的值要和发送通知页面的name值相同,用来区分哪一个通知 // object 的值为nil时,代表监听任何对象发送的通知,下文中用的是object:obj1 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"changeValue" object:nil]; } - (void)changeValue:(NSNotification *)notifi{ // 可以传多值 if ([notifi.object isEqualToString:@"obj1"]) { NSString *str = [NSString stringWithFormat:@"%@--%@",[[notifi userInfo] objectForKey:@"value1"],[[notifi userInfo] objectForKey:@"value2"]]; _lbValue.text = str; }else { _lbValue.text = @"aaaaaa"; } } // 按钮点击事件 - (IBAction)nextAction:(UIButton *)sender{ NextViewController *nextController = [[NextViewController alloc] init]; [self presentViewController:nextController animated:YES completion:nil]; }
- (void)viewDidLoad { [super viewDidLoad]; } // 按钮点击事件 - (IBAction)finishAction:(UIButton *)sender{ // 发送通知 // userInfo 为附加属性,为字典类型 [[NSNotificationCenter defaultCenter] postNotificationName:@"changeValue" object:@"obj1" userInfo:@{@"value1":_tfText.text,@"value2":@"111111"}]; [self dismissViewControllerAnimated:YES completion:nil]; }
代理传值
@interface ViewController () <ChangeValueDelegte> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)nextAction:(id)sender{ NextViewController *nextController = [[NextViewController alloc] init]; // 遵守协议 nextController.delegate = self; [self presentViewController:nextController animated:YES completion:nil]; } // 实现协议中的方法 - (void)shoudChangeValue:(NSString *)paraStr{ _lbValue.text = paraStr; }
#import <UIKit/UIKit.h> // 声明一个协议 @protocol ChangeValueDelegte <NSObject> - (void)shoudChangeValue:(NSString *)paraStr; @end @interface NextViewController : UIViewController @property (nonatomic,weak) IBOutlet UITextField *tfValue; // 注意delegate用weak修饰,为防止循环引用 @property (nonatomic,weak) id<ChangeValueDelegte> delegate; @end - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)finishAction:(id)sender{ // 让代理传值 [_delegate shoudChangeValue:_tfValue.text]; [self dismissViewControllerAnimated:YES completion:nil]; }
block传值
- (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)nextAction:(id)sender{ NextViewController *nextController = [[NextViewController alloc] init]; // 实现block [nextController setChangeValue:^(NSString *param){ _lbValue.text = param; }]; [self presentViewController:nextController animated:YES completion:nil]; }
#import <UIKit/UIKit.h> // 声明一个block typedef void(^ShoudChangeValue)(NSString *paraStr); @interface NextViewController : UIViewController @property (nonatomic,weak) IBOutlet UITextField *tfValue; // block 用copy修饰 @property (nonatomic,copy) ShoudChangeValue changeValue; @end - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)finishAction:(id)sender{ // 调用block传值 _changeValue(_tfValue.text); [self dismissViewControllerAnimated:YES completion:nil]; }
相关文章推荐
- ios 单一线程中的Runloop机制会导致线程安全问题吗?
- iOS AVAudioSession、AudioSession Category选择和声音通道听筒、扬声器切换
- IOS学习之 手势操作
- CAKeyframeAnimation-核心动画-帧动画
- ios 使用block中使用self可能产生的循环引用
- NSUserDefaults 存储可变数组问题
- iOS提交后申请加急审核
- iOS中Bonjour:NSNetServiceBrowser在子线程上使用
- wxhl iOS bj 49 张浩 学习心得文章连载 第14章
- iOS开发总结之代码规范
- 蓝懿学习 练习总结
- <iOS>数据缓存
- 【iOS程序启动与运转】- RunLoop个人小结
- IOS开发基础知识--碎片24
- iOS 懒加载模式
- iOS 开发必备的调试利器
- ios 点击按钮,显示遮盖层的效果
- iOS warning
- ios 底部弹出框菜单
- iOS 代码添加约束—VFL (AutoLayout)