iOS:OC--属性传值,代理传值,通知传值

先创建导航栏视图,再新建两个类MainViewController(一级界面)和SecondViewController(二级界面)

  • 正向属性传值,反向代理传值

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //创建window
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[MainViewController new]];
    [self.window makeKeyWindow];
    return YES;
}

MainViewController.h

#import 
@interface MainViewController : UIViewController

@property(nonatomic,strong)UITextField* textFld1;

@end

MainViewController.m

#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    [self creatUI];
    
}
#pragma mark --- creatUI
-(void)creatUI
{
    self.textFld1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    self.textFld1.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_textFld1];
    
    UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);
    [pushButton setTitle:@"push" forState:(UIControlStateNormal)];
    [pushButton addTarget:self action:@selector(PushClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pushButton];
}

#pragma mark --- 按钮点击事件
-(void)PushClick:(UIButton *)sender
{
    SecondViewController *secondVC = [SecondViewController new];
    
    secondVC.lableString = self.textFld1.text;
    
    secondVC.delegate1 = self;
    
    [self.navigationController pushViewController:secondVC animated:YES];
}

#pragma mark --- 实现协议
-(void)byValue:(NSString *)sender{
    self.textFld1.text = sender;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

SecondViewController.h

#import 
#import "VCPro.h"
@interface SecondViewController : UIViewController

@property(nonatomic,assign)id delegate1;

@property(nonatomic,copy)NSString *lableString;

@property(nonatomic,strong)UITextField* textFld2;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "MainViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    [self creatUI];
}

#pragma mark --- creatUI
-(void)creatUI
{
    self.textFld2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    self.textFld2.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_textFld2];
    
    UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);
    [pushButton setTitle:@"pop" forState:(UIControlStateNormal)];
    [pushButton addTarget:self action:@selector(PopClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pushButton];
    _textFld2.text = self.lableString;
}

#pragma mark --- 按钮点击事件
-(void)PopClick:(UIButton *)sender
{
    [self.delegate1 byValue:_textFld2.text];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

VCPro.h

#import 

@protocol VCPro 

-(void)byValue:(NSString *)sender;

@end
  • 通知传值

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //创建window
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[FirstViewController new]];
    [self.window makeKeyWindow];
    return YES;
}

FirstViewController.m

#import "FirstViewController.h"
#import "SencedViewController.h"
@interface FirstViewController ()

@property(nonatomic,strong)UITextField *textF;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    [self creatUI];
    //第二步:谁接受消息,谁注册通知(注册监听者)
    //注册监听者
    //addObserver:监听者,一般都是控制器本身去监听一个通知中心
    //selector:当监听到通知中心发送的通知,执行此方法
    //name:通知中心名字,和要发送通知的对象名一样
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeTextF:) name:@"ChangeValue" object:nil];
}
-(void)ChangeTextF:(NSNotification *)sender
{
    NSDictionary *dict = sender.userInfo;
    self.textF.text = dict[@"Data"];
}
-(void)creatUI
{
    self.textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    self.textF.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textF];
    //按钮
    UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    pushButton.frame = CGRectMake(0, 140, self.view.frame.size.width, 30);
    pushButton.backgroundColor = [UIColor cyanColor];
    [pushButton setTitle:@"push" forState:(UIControlStateNormal)];
    [pushButton addTarget:self action:@selector(PushCliuck:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pushButton];
}
-(void)PushCliuck:(UIButton *)sender
{
    SencedViewController *secondVC = [SencedViewController new];
    //跳转
    [self.navigationController pushViewController:secondVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

SencedViewController.h

#import 

@interface SencedViewController : UIViewController

#pragma mark - 通知传值
/*
 通知传值:
 1.需要发送通知的物体
 2.接受者:谁监听谁接收(注册通知 -> name前后一定要一样)
 3.实现通知内部方法,并实现传值
 4.消息发送完成,移除监听者
 */

@end

SencedViewController.m

#import "SencedViewController.h"

@interface SencedViewController ()
@property(nonatomic,strong)UITextField *textF;
@end

@implementation SencedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.view.backgroundColor = [UIColor cyanColor];
    [self creatUI];
}
-(void)creatUI
{
    self.textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    self.textF.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textF];
    //按钮
    UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    pushButton.frame = CGRectMake(0, 140, self.view.frame.size.width, 30);
    pushButton.backgroundColor = [UIColor orangeColor];
    [pushButton setTitle:@"push" forState:(UIControlStateNormal)];
    [pushButton addTarget:self action:@selector(PopCliuck:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pushButton];
}
-(void)PopCliuck:(UIButton *)sender
{
    //发送通知
    //postNotificationName:通知中心名字
    //object:接收对象
    //userInfo:携带参数
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Data":self.textF.text}];
    
    //跳转
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(iOS:OC--属性传值,代理传值,通知传值)