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 <UIKit/UIKit.h>
@interface MainViewController : UIViewController

@property(nonatomic,strong)UITextField* textFld1;

@end

MainViewController.m

#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<VCPro>

@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 <UIKit/UIKit.h>
#import "VCPro.h"
@interface SecondViewController : UIViewController

@property(nonatomic,assign)id<VCPro> 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 <Foundation/Foundation.h>

@protocol VCPro <NSObject>

-(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 <UIKit/UIKit.h>

@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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342

推荐阅读更多精彩内容