一.关于页面的跳转

1.首先创建一个工程,再添加一个文件命名为nextViewController
2.ViewController.m文件

#import "ViewController.h"
#import "nextViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation ViewController
-(UILabel *)label{
    if(_label==nil)
    {
        _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _label.backgroundColor=[UIColor blackColor];
        _label.textColor=[UIColor whiteColor];
        _label.font=[UIFont systemFontOfSize:20];
    }
    return _label;
}
-(UIButton *)btn{
    if(_btn==nil)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面2" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    nextViewController *nextVC=[[nextViewController alloc]init ];
    [self presentViewController:nextVC animated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.label];
    [self.view addSubview:self.btn];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

3.nextViewController

#import "nextViewController.h"

@interface nextViewController ()
@property (strong,nonatomic) UITextField *textField;
@property (strong,nonatomic) UIButton *btn;
@end

@implementation nextViewController
-(UITextField *)textField{
    if(!_textField)
    {
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _textField.textColor=[UIColor whiteColor];
        _textField.font=[UIFont systemFontOfSize:20];
        _textField.borderStyle=UITextBorderStyleLine;
    }
    return _textField;
}
-(UIButton *)btn{
    if(!_btn)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面1" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

二.属性传值
ViewController.m

#import "ViewController.h"
#import "nextViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation ViewController
-(UILabel *)label{
    if(_label==nil)
    {
        _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _label.backgroundColor=[UIColor yellowColor];
        _label.textColor=[UIColor redColor];
        _label.font=[UIFont systemFontOfSize:20];
    }
    return _label;
}
-(UIButton *)btn{
    if(_btn==nil)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面2" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    nextViewController *nextVC=[[nextViewController alloc]init ];
    //属性传值--传递
    nextVC.str=@"属性传值";
    
    [self presentViewController:nextVC animated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.label];
    [self.view addSubview:self.btn];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

                nextViewController.h
#import <UIKit/UIKit.h>
@interface nextViewController : UIViewController
//定义一个字符串属性
@property (strong,nonatomic) NSString *str;
@end


                   nextViewController.m
#import "nextViewController.h"

@interface nextViewController ()
@property (strong,nonatomic) UITextField *textField;
@property (strong,nonatomic) UIButton *btn;
@end

@implementation nextViewController
-(UITextField *)textField{
    if(!_textField)
    {
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _textField.textColor=[UIColor blackColor];
        _textField.font=[UIFont systemFontOfSize:20];
        _textField.borderStyle=UITextBorderStyleLine;
        //属性传值--接收并显示
        _textField.text=self.str;
    }
    return _textField;
}
-(UIButton *)btn{
    if(!_btn)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面1" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

三.单例传值
File-New-File-Cocoa Touch Class,命名为DefaultInstance.h;

                  DefaultInstance.h
#import <Foundation/Foundation.h>

@interface DefaultInstance : NSObject
@property(nonatomic,weak)NSString *str;
+(instancetype)sharedInstance;
@end
                  DefaultInstance.m

#import "DefaultInstance.h"

@implementation DefaultInstance

//通过类方法创建单例对象
+(instancetype)sharedInstance
{
    static DefaultInstance   *sharedVC=nil;
    if(sharedVC==nil)
    {
        sharedVC=[[DefaultInstance alloc]init];
    }
    return sharedVC;
}
@end

                   ViewController.m
#import "ViewController.h"
#import "nextViewController.h"
#import "DefaultInstance.h"
@interface ViewController ()
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation ViewController
-(UILabel *)label{
    if(_label==nil)
    {
        _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _label.backgroundColor=[UIColor yellowColor];
        _label.textColor=[UIColor redColor];
        _label.font=[UIFont systemFontOfSize:20];
    }
    return _label;
}
-(UIButton *)btn{
    if(_btn==nil)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面2" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    nextViewController *nextVC=[[nextViewController alloc]init ];
    //属性传值--传递
    [DefaultInstance sharedInstance].str=@"单例传值";
    
    [self presentViewController:nextVC animated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.label];
    [self.view addSubview:self.btn];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //单例的反向传值--接受并显示
    self.label.text=[DefaultInstance sharedInstance].str;
}

@end

               nextViewController.m
#import "nextViewController.h"
#import "DefaultInstance.h"
@interface nextViewController ()
@property (strong,nonatomic) UITextField *textField;
@property (strong,nonatomic) UIButton *btn;
@end

@implementation nextViewController
-(UITextField *)textField{
    if(!_textField)
    {
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _textField.textColor=[UIColor blackColor];
        _textField.font=[UIFont systemFontOfSize:20];
        _textField.borderStyle=UITextBorderStyleLine;
        //单例传值--接收并显示
        _textField.text=[DefaultInstance sharedInstance].str;
    }
    return _textField;
}
-(UIButton *)btn{
    if(!_btn)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面1" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    //单例的反向传值--传递
    [DefaultInstance sharedInstance].str=self.textField.text;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

四.NSUserDefaults传值
这里写图片描述

                      ViewController.m
#import "ViewController.h"
#import "nextViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation ViewController
-(UILabel *)label{
    if(_label==nil)
    {
        _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _label.backgroundColor=[UIColor yellowColor];
        _label.textColor=[UIColor redColor];
        _label.font=[UIFont systemFontOfSize:20];
    }
    return _label;
}
-(UIButton *)btn{
    if(_btn==nil)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面2" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    nextViewController *nextVC=[[nextViewController alloc]init ];
    //NSUserDefaults传值--传递
    [[NSUserDefaults standardUserDefaults]setObject:@"NSUserDefaults传值" forKey:@"NSUserDefaults"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self presentViewController:nextVC animated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.label];
    [self.view addSubview:self.btn];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //NSUserDefaults的反向传值--接受并显示
    self.label.text=[[NSUserDefaults standardUserDefaults]objectForKey:@"NSUserDefaults-re"];
  
}

@end

             nextViewController.m
#import "nextViewController.h"
@interface nextViewController ()
@property (strong,nonatomic) UITextField *textField;
@property (strong,nonatomic) UIButton *btn;
@end

@implementation nextViewController
-(UITextField *)textField{
    if(!_textField)
    {
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _textField.textColor=[UIColor blackColor];
        _textField.font=[UIFont systemFontOfSize:20];
        _textField.borderStyle=UITextBorderStyleLine;
        //NSUserDefualts传值--从文件中读取并显示
        _textField.text=[[NSUserDefaults standardUserDefaults]objectForKey:@"NSUserDefaults"];
    }
    return _textField;
}
-(UIButton *)btn{
    if(!_btn)
    {
        _btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _btn.backgroundColor=[UIColor redColor];
        [_btn setTitle:@"跳转至页面1" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//btn点击事件--跳转至页面2
-(void)btnClick{
    //NSUserDefaults的反向传值--传递
    [[NSUserDefaults standardUserDefaults]setObject:self.textField.text forKey:@"NSUserDefaults-re"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

五.代理传值
这里写图片描述

               1. 在nextViewController.h中写如图所示代码

这里写图片描述

         2.nextViewController.m

这里写图片描述
3.ViewController.m
这里写图片描述
这里写图片描述
这里写图片描述

六.block 传值

                   nextViewController.h

这里写图片描述

                   nextViewController.m

这里写图片描述

                     ViewController.m

这里写图片描述
七.通知传值

这里写图片描述

                       页面2

这里写图片描述
这里写图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐