UIday0802:UINavigationController 传值(界面通信)

来源:互联网 发布:java登录界面设计代码 编辑:程序博客网 时间:2024/06/05 21:51

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
原创粉丝点击