przekazać zmienną NSString do innej klasy za pomocą NSNotification
Chcę przekazać NSString z jednej klasy do innej klasy i dodać tę NSString do NSMutableArray w mojej drugiej klasie. Wierzę, że mogę użyć NSNotification do tego, ale nie wiem, jak przekazać zmienną nad powiadomieniem. Mój kod wyglądałby mniej więcej tak:
//class1.h
<code>#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(strong,nonatomic)NSString *variableString; @end </code>
//class1.m
<code>#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize variableString = _variableString; - (void)viewDidLoad { [super viewDidLoad]; [self setVariableString:@"test"]; [[NSNotificationCenter defaultCenter] postNotificationName: @"pasteString" object: _variableString]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } @end </code>
//class2.h
<code>#import <UIKit/UIKit.h> @interface ViewController2 : UIViewController @property(strong,nonatomic)NSMutableArray *arr; @end </code>
//class2.m
<code>#import "ViewController2.h" @interface ViewController2 () @end @implementation ViewController2 @synthesize arr = _arr; - (void)viewDidLoad:(BOOL)animated { [super viewDidLoad]; if(_arr == nil) { _arr = [[NSMutableArray alloc]init]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"pasteString" object:nil]; // Do any additional setup after loading the view. } - (void) incomingNotification:(NSNotification *)notification{ NSString *theString = [notification object]; [_arr addObject:theString]; } @end </code>