Pass NSString variable to another class using NSNotification

I want to pass an NSString from one class to another class and add an NSString to an NSMutableArray in my second class. I believe that I can use NSNotification for this, but I don’t know how to pass the variable through a notification. My code would be something like this:

//Class1.h

#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(strong,nonatomic)NSString *variableString; @end 

//class1.m

 #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 

//class2.h

 #import <UIKit/UIKit.h> @interface ViewController2 : UIViewController @property(strong,nonatomic)NSMutableArray *arr; @end 

//class2.m

 #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 
+7
source share
2 answers

In the sender class, you can send a notification with an object with something like this:

 [[NSNotificationCenter defaultCenter] postNotificationName: NOTIFICATION_NAME object: myString]; 

The listener or recipient class must register to notify:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:NOTIFICATION_NAME object:nil]; 

IncomingNotification Method:

 - (void) incomingNotification:(NSNotification *)notification{ NSString *theString = [notification object]; ... } 

EDIT

When you send a notification from "ViewController", does "ViewController2" load?

+38
source
 Send your Notification like this: if(isComingFromHowToUseThisApp) { [[NSNotificationCenter defaultCenter] postNotificationName:@"noteFromVideoPlayer" object:@"fromHowToUseThisApp"]; }else { [[NSNotificationCenter defaultCenter] postNotificationName:@"noteFromVideoPlayer" object:@"fromVideoPlayerViewiPad"]; } Receive your notification like this: // Reset the default value of table view in master split view - (void)defaultCellSelectionOfTableView:(NSNotification *) objNotification { // We need to change the default selection of row NSString *noticeFromVideoPlayer = [objNotification object]; }// end defaultCellSelectionOfTableView 
0
source

All Articles