内容总结自慕课网课程[iOS界面间传值继续学习](https://www.imooc.com/learn/863)

  1. 属性传值即通过UiViewController的实例的属性的赋值&取值进行值的传递

    1
    2
    3
    4
    5
    6
    7
    8
    
       
    #import <UIKit/UIKit.h>
       
    @interface NextViewController : UIViewController
    @property(nonatomic, strong) NSString *str;
       
       
    @end
    

    nextC.str=@"你好";

  2. 单例传值即类似JAVA中的静态变量进行传值

  3. NSUserDefaults传值通过系统的NSUserDefaults进行传值

1
2
3
4
5
6
7
8
   NextViewController *nextC = [[NextViewController alloc] init];


   [[NSUserDefaults standardUserDefaults] setObject:@"啦啦啦" forKey:@"key"];
   [[NSUserDefaults standardUserDefaults] synchronize];

   [self presentViewController:nextC animated:YES completion:nil];
}
```
self.tfield.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
```
  1. 代理传值即类似JAVA中的运用接口进行传值

    1
    2
    3
    4
    5
    
    @protocol PassValueDel <NSObject>
       
    - (void)pasValue:(NSString *)str;
       
    @end
    

    @interface ViewController () <PassValueDel>

  2. Block传值

    1. 定义block@property(copy) void (^block)(NSString *);
    2. 传值self.block(@"nidsfdsafasdfasdf");
    3. 接收 nextC.block = ^(NSString *str) { self.label.text = str; };
  3. 通知传值

  4. 监听通知

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    - (void)btnClick {
        NextViewController *nextC = [[NextViewController alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(msgHandler:) name:@"ls" object:nil];
        [self presentViewController:nextC animated:YES completion:nil];
    }
         
    - (void)msgHandler:(NSNotification *)msg {
        self.label.text = msg.userInfo[@"not"];
         
    }
    
  5. 发送通知

    1
    2
    3
    4
    5
    
    - (void)btnClick {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ls" object:nil userInfo:@{@"not": @"我很帅"}];
       
        [self dismissViewControllerAnimated:YES completion:nil];
    }