我想将NSString从一个类传递到另一个类,并将NSString添加到我的第二个类中的NSMutableArray.我相信我可以使用NSNotification,但我不知道如何通过变量通知.我的代码是这样的:
//class1.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)NSString *variableString;
@end
Run Code Online (Sandbox Code Playgroud)
//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
Run Code Online (Sandbox Code Playgroud)
//class2.h
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property(strong,nonatomic)NSMutableArray *arr;
@end
Run Code Online (Sandbox Code Playgroud)
//class2.m
#import "ViewController2.h" …Run Code Online (Sandbox Code Playgroud)