#import "FirstViewController.h"
#import "SecondViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    初始化视图控制器
    FirstViewController *firstVc=[[FirstViewController alloc] init];
//    将视图控制器放单navc中当根视图控制器
    UINavigationController *navc=[[UINavigationController alloc] initWithRootViewController:firstVc];
   // navc.tabBarController.tabBar.tintColor=[UIColor redColor];
    navc.tabBarItem.title=@"first";
    //navc.tabBarItem.badgeValue=@"1";
    navc.tabBarItem.image=[UIImage imageNamed:@"compose_camerabutton_background_highlighted"];
   
   
    SecondViewController *secondVc=[[SecondViewController alloc] init];
   
    UINavigationController *navcSecond=[[UINavigationController alloc] initWithRootViewController:secondVc];
   
    navcSecond.tabBarItem.title=@"second";
    navcSecond.tabBarItem.badgeValue=@"2";
    navcSecond.tabBarItem.image=[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"];
   
    UITabBarController *tabController=[[UITabBarController alloc] init];
    tabController.tabBar.tintColor=[UIColor blackColor];
    [tabController setViewControllers:@[navc,navcSecond]];

//    将navc设置成window的根视图
    self.window.rootViewController=tabController;
   
   
   
    return YES;
 
}
/////////////////////
 
 
 
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController<UITextFieldDelegate,postValueDelegate>//遵循协议
@property(strong,nonatomic) UITextField *txtName;
 
/////////////////////////
 
/**
  1.页面之间传值方式
    属性传值
    适用于  正向传值
    1.1 在要显示信息的页面创建属性
    1.2 在要传值的页面 设置属性值
    1.3 在显示信息的页面的viewDidLoad方法中 接收属性值
 */

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    初始化文本框
    self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
    self.txtName.borderStyle=1;
    self.txtName.delegate=self;
    [self.view addSubview:self.txtName];
   
   
    self.view.backgroundColor=[UIColor redColor];
    self.title=@"firstVC";
//    设置导航栏的前景色
    self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor yellowColor]};
   
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithTitle:@"next" style:2 target:self action:@selector(test)];
//    设置 UIBarButtonItem的前景色
    rightItem.tintColor=[UIColor redColor];
    self.navigationItem.rightBarButtonItem=rightItem;
   
   
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"card_icon_addattention"] style:2 target:self action:@selector(test)];
    leftItem.tintColor=[UIColor greenColor];
    self.navigationItem.leftBarButtonItem=leftItem;
   
}
-(void)test
{
    SecondViewController *second=[[SecondViewController alloc]init ];
    second.str=self.txtName.text;
   
   
//    指定代理
    second.delegate=self;
   
   
         [self.navigationController pushViewController:second animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.txtName resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    键盘隐藏
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
    // Dispose of any resources that can be recreated.
}


//实现 协议方法  (接收值)
-(void)postVaue:(NSString *)str
{
    self.txtName.text=str;
}
 
///////////////////
#import <UIKit/UIKit.h>

//创建协议  声明传值的协议方法
@protocol postValueDelegate <NSObject>

-(void)postVaue:(NSString *)str;

@end


@interface SecondViewController : UIViewController
//属性
@property(strong,nonatomic) NSString *str;
@property(strong,nonatomic) UITextField *txtName;

//声明协议类型的属性
@property(assign,nonatomic) id<postValueDelegate> delegate;
@end
 
 
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor=[UIColor greenColor];
    self.title=self.str;
   
   
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"back" style:2 target:self action:@selector(backPage)];
   
   
    self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(200, 200, 150, 44)];
   
    self.txtName.backgroundColor=[UIColor purpleColor];
    self.txtName.borderStyle=1;
   
    [self.view addSubview:self.txtName];
    self.txtName.text=self.str;
   
   
}

//代理方法
-(void)backPage
{
//
    if (self.delegate) {
        [self.delegate postVaue:self.txtName.text];
    }
   
    [self.navigationController popViewControllerAnimated:YES];
}