Ios 使用通知将NSString值从一个ViewController传递到另一个ViewController?

Ios 使用通知将NSString值从一个ViewController传递到另一个ViewController?,ios,nsnotificationcenter,nsnotification,Ios,Nsnotificationcenter,Nsnotification,如何在iOS中使用通知将NSString值从ViewController1传递到ViewController2?我试过: 视图控制器1 - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"test" object:nil]; } - (void) incomingNotific

如何在iOS中使用通知将NSString值从
ViewController1
传递到
ViewController2
?我试过:

视图控制器1

- (void)viewDidLoad
{
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"test" object:nil];

 }
    - (void) incomingNotification:(NSNotification *)notification{
        NSString *theString = [notification object];
        NSLog(@"theString value=%@",theString);
    }
视图控制器2

- (void)viewDidLoad
{
     NSString *myString=@"testing";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object: myString];
}
当我转到ViewController2时,它工作正常


但是我想使用这些通知将值从
ViewController1
发送到
ViewController2
。有可能吗。怎么做?

我认为没有必要调用不可见的viewController来发送通知。您可以按如下方式传递值:

如果使用故事板,可以在ViewController1的方法
prepareforsgue
中获得ViewController2,因此可以在此处传递引用

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var targetViewController = segue.destinationViewController as UIViewController
}
如果您手动创建第二个ViewController并使用
presentViewController
,只需在初始化ViewController2时传递引用即可



如果你坚持使用通知。将view2设置为通知发送者,view1设置为另一个通知的接收者。

您会将NSNotification对象传递给您的函数

将字符串或其他对象放入字典中

NSDictionary* userInfo = @{@"completionHandler" : completionHandler,
                           @"myStringIdentifier" : myString};

NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"myNotification" object:self userInfo:userInfo];


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:) name:@"UIKeyboardWillHideNotification" object:nil];

-(void) receiveNotification:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"MyNotification"])
    {
       NSDictionary* userInfo = notification.userInfo;
       ... do dictionary stuff
    }
}

这是一个在搜索userinfo和NSNotification以获取更多示例之前提出的问题。

根据文档,是的,这是可能的。但是你应该先看一下文件(参见)

你应该看的方法是

- (void)postNotificationName:(NSString *)notificationName
                  object:(id)notificationSender
                userInfo:(NSDictionary *)userInfo
在哪里

notificationName
是您要发布的通知。
notificationSender
是要发布通知的对象,
userInfo
是要随通知传递的信息

因此,如果要将通知从
ViewController2
传递到
ViewController1
(或反之亦然)

在这里,我要强调两件主要的事情

  • 两个控制器都应该是“有效的”。换句话说,它们应该被分配到内存中,并呈现在某个地方
  • 添加观察者时,还应将其删除
  • 更新1

    - (void)viewDidLoad
    {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"test" object:nil];
    
     }
        - (void) incomingNotification:(NSNotification *)notification{
            NSString *theString = [notification object];
            NSLog(@"theString value=%@",theString);
        }
    
    在本例中,您正在将字符串从
    UIViewController2
    传递到
    UIViewController1
    。只需切换正在使用的代码,就可以将相同的字符串从
    ViewController1
    传递到
    ViewController2

    我的问题:你的目标是什么?

    这会奏效

    在ViewController 2中

    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self userInfo:@{@"stringValue": myString}];
    
    在ViewController 1中

    - (void)viewDidLoad
    {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"test" object:nil];
    
    }
    - (void) incomingNotification:(NSNotification *)notification{
    
        NSString *theString = notification.userInfo[@"stringValue"] ;
    
        NSLog(@"theString value=%@",theString);
    }
    

    您可以通过在ViewController1的on button click事件中初始化ViewController2来使用它。要调用的步骤, 1.在ViewController2中创建方法(比如notifRegister),该方法初始化通知以接收字典值。 2.在按钮上单击event init ViewController2并调用方法notifRegister 3.然后在ViewController1中发布通知

    例如:

     - (IBAction)navigation:(id)sender {
    
        ViewController1 *vc=  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController2"];
        [vc notifRegister];
        [self.navigationController pushViewController:(UIViewController*)vc animated:YES];
    
        NSDictionary *name=[[NSDictionary alloc] initWithObjectsAndKeys:@"Tommy",@"name",nil];
          [[NSNotificationCenter defaultCenter] postNotificationName:@"TextChanged" object:name];
    }
    
    在ViewController2上:

        -(void)notifRegister
    {
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadLabel:) name:@"TextChanged"object:nil];
    
    }
    -(void)reloadLabel:(NSNotification*)sender
    {
        NSLog(@"reload label called %@",sender.object);
    
        self.labelName=[sender.object objectForKey:@"name"];
    }
    

    这里self.labelName是一个NSString对象…

    为什么?您很可能不应该使用通知。您希望何时发送这些消息?出于学习目的,我需要知道通知。这就是我使用通知的原因。是否可能?如何将view2设置为通知发送者,将view1设置为另一个通知的接收者?不要使用相同的通知名,也不要将
    postNotificationName
    方法放在
    viewDidLoad
    方法,因为尚未创建接收器。谢谢@Ryan Heitner。但我需要知道我将此代码放在了哪个ViewController中。请清楚地告诉Ryan HeitnerAn NSNotification可以放在任何对象中。您从一个对象发送通知,您可以通过观察者在一个或多个其他位置接收通知。尝试一个教程,如获得更好的理解。在发布问题之前,您应该尽可能多地做作业,以避免被拒绝。好的,我如何在viewcontroller2@flexaddicted@Tabi中获取NSString值。我可以看到注释。在这种情况下,您将从视图控制器2传递到视图控制器1。