IOS学习之——ViewController之间正向传值

下面定义两个页面,为了简单起见,就不添加Btton进行页面跳转,直接点击屏幕,触发

touchesBegan事件,进行页面跳转。


//
//  OneViewController.m
//  双向传值
//
//  Created by spare on 16/4/16.
//  Copyright © 2016年 spare. All rights reserved.
//

#import "OneViewController.h"
#import "TwoViewController.h"

@interface OneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField1;

@end

@implementation OneViewController

//点击屏幕任意位置,进入页面2
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //初始化将要进入的页面
    TwoViewController *vc2=[[TwoViewController alloc]init];
    //通过页面2定义的Content属性,将值传给页面2
    vc2.content=self.textField1.text;
    [self presentViewController:vc2 animated:YES completion:nil];

    }

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (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



#import <UIKit/UIKit.h>

@interface TwoViewController : UIViewController
//页面1的对象不能直接访问页面2的textField属性
//需要定义一个中间的变量值,将页面一的值保存在中间变量中
@property(nonatomic,copy)NSString *content;

@end



//
//  TwoViewController.m
//  双向传值
//
//  Created by spare on 16/4/16.
//  Copyright © 2016年 spare. All rights reserved.
//

#import "TwoViewController.h"

@interface TwoViewController ()

@property (weak, nonatomic) IBOutlet UITextField *textField2;

@end

@implementation TwoViewController

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

//每次进入页面的时候,都去刷新获得的值
-(void)viewWillAppear:(BOOL)animated{
    //通过中间属性,传值给textField2;
    self.textField2.text=self.content;
}

- (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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值