передать переменную NSString в другой класс с помощью NSNotification

Я хочу передать NSString из одного класса в другой класс и добавить этот NSString в NSMutableArray во втором классе.. Я считаю, что могу использовать NSNotification для этого, но я не знаю, как передать переменную через уведомление. Мой код будет примерно таким:

//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
задан Niels Sønderbæk 22 April 2012 в 23:44
поделиться

0 ответов