UIday0802:UINavigationController 传值(界面通信)

UINavigationController 传值(界面通信)


AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    RootViewController * rootVC = [[RootViewController alloc]init];
    
    //创建导航控制器 (它主要控制controller,而不直接控制视图)
    //导航控制器管理视图控制器,创建导航控制器需要一个视图控制器作为根视图控制器
    UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    
    //将导航控制器作为window的根视图控制器
    //所有视图控制器都可以作为window的根视图控制器
    self.window.rootViewController = rootNC;
    
    return YES;
}

<pre name="code" class="objc">@end


 

RootViewController.m

#import "RootViewController.h"
#import "NextViewController.h"

//  遵循协议
@interface RootViewController ()<NextViewControllerDelegate>

@property(nonatomic,strong) UITextField * rootTextField;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    //右button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItemAction:)];
    
    //创建textField
    self.rootTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 250, 50)];
    self.rootTextField.backgroundColor = [UIColor greenColor];
    [self.view addSubview: _rootTextField];
    
    
    // 传值 5种传值方式
    /*
     1、属性传值  用于从第一个页面传递给第二个页面
     2、代理传值  将第二个页面的值传递个第一个页面
     
     3、block传值   好些不好理解
     4、单例传值  理解起来跟属性传值差不多
     5、通知传值  消耗资源最大的一种传值方式     
     */
    
}

-(void)rightBarButtonItemAction:(UIBarButtonItem *)sender{
    
    //下一页获取前一页的值
    NextViewController * nextVC = [[NextViewController alloc]init];
    nextVC.aString = self.rootTextField.text;
    
    //设置代理
    nextVC.delegate = self;
    [self.navigationController pushViewController:nextVC animated:YES];
}

//实现代理方法
-(void)passValue:(NSString *)string{
    //接收第二个页面传过来的值
    self.rootTextField.text = string;
}

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

/*


@end

NextViewController.h

#import <UIKit/UIKit.h>

//第一个页面做第二个页面的代理
@protocol NextViewControllerDelegate <NSObject>

//写一个方法,无返回值  有参数
//参数就是要传的值
-(void)passValue:(NSString *)string;


@end

@interface NextViewController : UIViewController

//属性传值 需要在第二个页面的controller.h 文件中定义属性用来接收值。
@property(nonatomic,strong)NSString * aString;

//代理
@property(nonatomic,strong)id<NextViewControllerDelegate>delegate;

@end

NextViewController.m

#import "NextViewController.h"

@interface NextViewController ()

@property(nonatomic,strong)UITextField * nextTextField;

@end

@implementation NextViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    
    // 左button
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"上一页" style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItemAction:)];
    
    self.nextTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 250, 50)];
    self.nextTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_nextTextField];
}

//视图将要显示  

-(void)viewWillAppear:(BOOL)animated{
    // 将接收到的值赋值给textField
    self.nextTextField.text = self.aString;
    
 }


-(void)leftBarButtonItemAction:(UIBarButtonItem *)sender{
    
    //在POP之前传值 代理必然有passValue方法  text作为参数传过去
    [self.delegate passValue:self.nextTextField.text];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

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

@end

注意:

<pre name="code" class="objc">//如果是第三个页面往第二个页面回传的话,这样写是不显示的
-(void)viewWillAppear:(BOOL)animated{
    self.firstTextField.text = self.string;
}

解决办法有三种:
// 解决办法1:
// 实现代理方法
-(void)passValue:(NSString *)str{
//    self.firstTextField.text = str; //将这种方法改为下面这种写法:
    self.string = str; //第一个string是在.h文件里定义接收text的变量,第二个是passValue方法传的变量
}

// 解决办法2:
//直接写到viewdidload里

// 解决办法3:
  //做判断,看是从第一个视图过来的还是第二个视图过来的

 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值