• 页面之间传值(单例传值)(自己使用)


    首先创建2个新界面 , 然后创建一个类,如下图

    然后在AppDeleate.h

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

    然后在AppDeleate.m中

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        FirstViewController *firstvc=[[FirstViewController alloc]init];
        self.window.rootViewController=firstvc;
        return YES;
    }
    

     然后进入AppStatus.h

    #import <Foundation/Foundation.h>
    
    @interface AppStatus : NSObject
    
    @property(strong ,nonatomic)NSString *contextStr;
    +(AppStatus *)shareInstance;
    
    @end
    

     AppStatus.m

    #import "AppStatus.h"
    
    @implementation AppStatus
    
    @synthesize contextStr = _contextStr;
    
    static AppStatus *_instance = nil;
    
    +(AppStatus *)shareInstance
    {
        if (_instance==nil) {
            _instance=[[super alloc]init];
        }
        return _instance;
    }
    -(id)init
    {
        if (self=[super init]) {
            
        }
        return self;
    }
    
    
    @end
    

    FirstViewController.h中

    #import <UIKit/UIKit.h>
    #import "SecondViewController.h"
    #import "AppStatus.h"
    
    @interface FirstViewController : UIViewController<UITextFieldDelegate>
    
    @property(strong,nonatomic) UITextField *text1;
    @property(strong,nonatomic) UIButton *btn1;
    
    @end
    

     FirstViewController.m中

    #import "FirstViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor=[UIColor greenColor];
        
        self.text1=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 60)];
        
        self.text1.borderStyle=1;
        
        [self.view addSubview:self.text1];
        
        self.text1.delegate=self;
        self.btn1=[[UIButton alloc]initWithFrame:CGRectMake(130, 300, 150, 60)];
        
        [self.btn1 setTitle:@"下一页" forState:0];
        
        [self.btn1 setTitleColor:[UIColor blackColor ] forState:0];
        
        [self.btn1 addTarget:self action:@selector(nextpage) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:self.btn1];
        
        
    }
    
    
    
    -(void)nextpage
    {
        [AppStatus shareInstance].contextStr=self.text1.text;
        SecondViewController *secondvc=[[SecondViewController alloc]init];
        
        [self presentViewController:secondvc animated:YES completion:^{
            NSLog(@"ok");
        }];
    }
    
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        
        
        
        return YES;
    }
    
    
    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        
       
        
        self.text1.text=[AppStatus shareInstance].contextStr;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    

    SecondViewController.h

    #import <UIKit/UIKit.h>
    #import "AppStatus.h"
    
    @interface SecondViewController : UIViewController<UITextFieldDelegate>
    
    @property(strong,nonatomic) UITextField *text2;
    @property(strong,nonatomic) UIButton *btn2;
    
    @end
    

     SecondViewController.m

    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor redColor];
        
        self.text2=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 60)];
        
        self.text2.borderStyle=1;
        
        [self.view addSubview:self.text2];
        
        self.text2.delegate=self;
        
        self.text2.text=[AppStatus shareInstance].contextStr;
        
        self.btn2=[[UIButton alloc]initWithFrame:CGRectMake(130, 300, 150, 60)];
        
        [self.btn2 setTitle:@"返回" forState:0];
        
        [self.btn2 setTitleColor:[UIColor colorWithRed:0.038 green:0.249 blue:1.000 alpha:1.000] forState:0];
        
        [self.btn2 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.btn2];
    }
    
    
    
    
    -(void)back
    {
        [AppStatus shareInstance].contextStr=self.text2.text;
         [self dismissViewControllerAnimated:YES completion:nil];
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
        
        return YES;
    }
    
    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        
        
        
        self.text2.text=[AppStatus shareInstance].contextStr;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    
  • 相关阅读:
    mybaits 时间查询DATE_FORMAT
    spring AOP
    Sqlserver 分页
    @PathVariable注解使用
    easyui 无限级数tree[menulist1 = GetMenuList(sm2,menulist1);]
    查询重复数据group by menu_id having count(menu_id)>1
    SQL把a表字段数据存到b表字段 update,,insert
    毕向东讲解(摘)—12.线程通信,解决安全问题
    URL的加密解密方法
    web项目中的浏览器行为和服务器行为
  • 原文地址:https://www.cnblogs.com/fume/p/5280871.html
Copyright © 2020-2023  润新知