pasar la variable NSString a otra clase con NSNotification
Quiero pasar un NSString de una clase a otra clase y agregar ese NSString a un NSMutableArray en mi segunda clase. Creo que puedo usar NSNotification para esto, pero no sé cómo pasar una variable por encima de la notificación. Mi código sería algo como esto:
//clase1.h
<code>#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(strong,nonatomic)NSString *variableString; @end </code>
//clase1.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>
//clase2.h
<code>#import <UIKit/UIKit.h> @interface ViewController2 : UIViewController @property(strong,nonatomic)NSMutableArray *arr; @end </code>
//clase2.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>