1. 程式人生 > >實現引數傳值的幾種方式

實現引數傳值的幾種方式

一、單例:
1.定義單例類
@interface single : NSObject
@property (nonatomic, copy) NSString *name;
+ (id)shareInstance;
@end
#import "single.h"

static single *instance = nil;


@implementation single

+ (id)shareInstance
{
    if (instance == nil) {
       
        instance = [[single alloc] init];
       
    }
    return instance;
}

@end

2.實現傳值

傳值者:
 single *s = [single shareInstance];
    s.name = _lable.text;

接收者:

//檢視將要顯示時呼叫
- (void)viewWillAppear:(BOOL)animated
{
    single *s1 = [single shareInstance];
    lable.text = s1.name;
}
二、通知:
 //取得輸入的資料
    NSString *text = _textFiekd.text;
   
    NSDictionary *dic = @{
                          @"key":text
                          };
   
    //傳送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeValue" object:self userInfo:dic];
   
    [self dismissViewControllerAnimated:YES completion:nil];




    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:@"changeValue" object:nil];
 

- (void)changeAction:(NSNotification *)notification {
    //取得傳遞的資料
    NSDictionary *dic = notification.userInfo;
    _label.text = dic[@"key"];
   
}

三、KVO觀察者模式
1.新增觀察者
     _secondCtrl = [[SecondViewController alloc] init];
    //監聽secondCtrl的屬性值變化
    [_secondCtrl addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
  

//接收到訊息後觸發的方法
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    NSLog(@"change:%@",change);
   
    NSString *text = [change objectForKey:@"new"];
    _label.text = text;
   
}
2.刪除觀察者
- (void)dealloc
{
    [_secondCtrl removeObserver:self forKeyPath:@"happyValue" context:NULL];
}


被觀察者
定義觀察屬性
@property(nonatomic, copy)NSString *text;
賦值
self.text = _textFiekd.text;

四、Block
1.要傳值的一邊定義block

typedef  void(^MyBlock)(NSString *text);


@property(nonatomic, copy)MyBlock block;
@property (weak, nonatomic) IBOutlet UITextField *textFiekd;

    _block(_textFiekd.text);


2.接收值的一邊

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
   
    secondCtrl.block = ^(NSString *text) {
   
        _label.text = text;
    };
}
五、代理

要傳值的一邊如下配置:
@protocol sendData <NSObject>

- (void)sendData:(NSString *)text;

@end

@property(nonatomic, assign)id<sendData> delegate;
@property (weak, nonatomic) IBOutlet UITextField *textFiekd;

- (IBAction)dismissAction:(id)sender {
   
    [_delegate sendData:_textFiekd.text];
   
    [self dismissViewControllerAnimated:YES completion:nil];
}

接收值的一邊如下配置:

@interface ViewController ()<sendData>
@property (weak, nonatomic) IBOutlet UILabel *label;


- (IBAction)presentAction:(id)sender {
    SecondViewController *secondCtrl = [[SecondViewController alloc] init];
   
    secondCtrl.delegate = self;
   
    [self presentViewController:secondCtrl animated:YES completion:nil];

}

- (void)sendData:(NSString *)text {

    _label.text = text;
   
}