iOS 关于传值方式的那些事
1.属性传值
-对方需要什么样类型的属性,就定义什么类型的,保持一致
-从一个控制器传递到下一个控制器,不能回传
(void)buttonAction:(UIButton *)button
{
SecondViewController *second =
[[SecondViewController alloc]init];//用下一个视图的属性接受想要传过去的值,属性传值
second.secondtext= _onetext;
[self.navigationController pushViewController:second animated:YES];
}
//页面跳转之后,就能在SecondViewController视图中,通过存值的属性,取用刚才传递过来的值:</span>
2.block传值
- 首先A控制器push B控制器 ,在B控制器中添加block属性,同时调用这个block。
-在A完成pushB之前,给你A的block属性,赋值,值为block块。块中,可以将B的属性传递给A,这个块只有调用的时候会执行。
//B控制器的.h文件
@property (nonatomic,copy) void (^NextViewControllerBlock)(NSString *tfText);
//B控制器的.m文件
@interface NextViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputTF;
@end
- (IBAction)BtnAction:(id)sender {
//判断block是否为空
if (self.NextViewControllerBlock) {
self.NextViewControllerBlock(self.inputTF.text);
}
[self.navigationController popViewController self Animated:YES];
}
//A控制器的.h文件
@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nextVCInfoLabel;
@end
//A控制器的.m文件
- (IBAction)btnClicked:(id)sender {
NextViewController *nextVC = [[NextViewController alloc]init];
nextVC.NextViewControllerBlock = ^(NSString *tfText){
self.nextVCInfoLabel.text = tfText;
};
[self.navigationController pushViewController:nextVC animated:YES];
}
2.通知传值
任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么,(用单粒模式调用到通知中心,post通知)
其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知,(执行某些操作)
通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知
- (void)postNotification:(NSNotification *)notification;
发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
发布一个名称为aName的通知,anObject为这个通知的发布者
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
observer:监听器,即谁要接收这个通知
aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
name:通知的名称
obj:通知发布者
block:收到对应的通知时,会回调这个block
queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃。
通知中心提供了相应的方法来取消注册监听器
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
实现过程:先注册消息 postNotificationName ,然后在响应的方法的接收消息 addObserver,最后实现接收消息的方法
通知传值时,需要向通知里,赋予字典,进行,传递 。同样举例说明:A控制器pushB控制器,在B控制器文本输入框输入字符串,输入完成后,通过通知中心发送通知,通知中携带字典信息,pop A控制器时,A控制器文本输入框显示B控制文本输入框输入的字符串。 A----one b---second ,下面时one控制器的.m文件
#import "OneViewController.h"
#import "SecondViewController.h"
@interface OneViewController ()
@property(nonatomic,retain)UILabel *lable;//用来存储通知字典里面的内容
@end
@implementation OneViewController
- (UILabel *)lable{
if (!_lable) {
_lable = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 300, 300)];
_lable.backgroundColor = [UIColor redColor];
}
return _lable;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"one";
[self.view addSubview:self.lable];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 30, 100, 100);
btn.tintColor = [UIColor redColor];
btn.backgroundColor = [UIColor blueColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(Pushsecond)forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(handlechangeLableText:) name:@"NotificationhandlechangeLableText"object:nil];
// Do any additional setup after loading the view.
}
- (void) Pushsecond {
SecondViewController *secondVC= [[SecondViewController alloc]init];
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)handlechangeLableText:(NSNotification *)notification{
NSDictionary *dic = notification.userInfo ;
self.lable.text = dic[@"test"];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
下面为second控制器的.m文件
#import "SecondViewController.h"
@interface SecondViewController ()<UITextFieldDelegate>
@property(nonatomic,retain)UITextField *textfield;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title =@"second";
self.view.backgroundColor = [UIColor colorWithRed:220/225 green:50/225 blue:100/225 alpha:0.8];
[self.view addSubview:self.textfield
];
// Do any additional setup after loading the view.
}
-(UITextField *)textfield{
if (!_textfield) {
_textfield = [[UITextField alloc]initWithFrame:CGRectMake(150,100, 220, 220)];
self.textfield.placeholder =@"请输入文字";
self.textfield.delegate = self;
}
return _textfield;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationhandlechangeLableText" object:self userInfo:@{
@"text":self.textfield.text}];
[self dismissViewControllerAnimated:self completion:nil];
return YES;
}