zoukankan      html  css  js  c++  java
  • 传值

    #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];
    }
  • 相关阅读:
    iOS 10 的一个重要更新-用 UIViewPropertyAnimator 编写动画
    iOS 10 的一个重要更新-线程竞态检测工具 Thread Sanitizer
    iOS 10 的一个重要更新-开发 iMessage 的第三方插件
    状态栏白色
    iOS 对 HTTPS 证书链的验证
    Xcode8 带来的新特性和坑
    升级cocoapods1.1.1版本
    (这也不是,那也不是,怎么办?)无论何时坚持自己的原则,当选准了目标,就不放弃,做最好的自己。我们必须要有一定的看法,正确的做法;有时,我们无法避免他人在旁的批评或劝告,但我们是无法取悦每个人的,否则将失去了方向,迷失了自己。
    笑话集锦(1)
    最感动的一首现代诗(转)(修正了一点翻译)
  • 原文地址:https://www.cnblogs.com/tianlianghong/p/5285439.html
Copyright © 2011-2022 走看看