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

首先创建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];
原文地址:https://www.cnblogs.com/fume/p/5280871.html