xcode 使用 NSNotification 将 NSString 变量传递给其他类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10272068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass NSString variable to other class with NSNotification
提问by Niels S?nderb?k
I want to pass a NSString from one class to another class and add that NSString to an NSMutableArray in my second class. I'm believe i can use NSNotification for this, but i don't know how to pass an variable over notification. My code would something like this:
我想将一个 NSString 从一个类传递到另一个类,并将该 NSString 添加到我的第二个类中的 NSMutableArray 中。我相信我可以为此使用 NSNotification,但我不知道如何通过通知传递变量。我的代码是这样的:
//class1.h
//class1.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)NSString *variableString;
@end
//class1.m
//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
//class2.h
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property(strong,nonatomic)NSMutableArray *arr;
@end
//class2.m
//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
回答by Jonathan Naguin
In sender class you can post a notification with an object with something like this:
在发件人类中,您可以发布带有类似对象的通知:
[[NSNotificationCenter defaultCenter] postNotificationName: NOTIFICATION_NAME object: myString];
The listener or receiver class has to register for the notification:
侦听器或接收器类必须注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:NOTIFICATION_NAME object:nil];
The method incomingNotification is:
方法incomingNotification是:
- (void) incomingNotification:(NSNotification *)notification{
NSString *theString = [notification object];
...
}
EDIT
编辑
When you post the notification from "ViewController", is "ViewController2" loaded?
当您从“ViewController”发布通知时,是否加载了“ViewController2”?
回答by Vinod Joshi
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