简体   繁体   English

查看多个导航控制器中的控制器通信

[英]view controllers communication in multiple Navigation Controllers

My app is using a tab bar controller with two tab bar items: Each item is a navigation controller. 我的应用程序使用带有两个标签栏项目的标签栏控制器:每个项目都是一个导航控制器。

I'm trying to access one view controller in one navigation controller from another view controller in the other navigation controller, is that possible?? 我正在尝试从另一个导航控制器中的另一个视图控制器访问一个导航控制器中的一个视图控制器,这可能吗? and How?? 如何??

I'd appreciate any possible help, Thanks. 谢谢您的帮助。

Mohsen 穆赫辛

You might consider using notifications or (even easier) Key-Value Observing. 您可能会考虑使用通知或(甚至更简单)键值观察。

I gave somebody some advice this morning about inter-controller communication in a UINavigationController context. 今天早上,我给了一些关于UINavigationController上下文中的控制器间通信的建议。 Then this afternoon I tried KVO for the first time, and I had to go back and give different advice. 然后今天下午我第一次尝试了KVO,我不得不回去并给出不同的建议。 KVO is WAY simpler than trying to chase things down in a view hierarchy. KVO比尝试按视图层次结构追踪事物更简单。 You just declare what's observing what, and then set that thing up to catch change notifications. 您只需声明正在观察的内容,然后将其设置为捕获更改通知即可。 Piece of cake! 小菜一碟!

EDIT: 编辑:

Notifications are probably the way to go here, actually--if you had both object instantiated in one place you could register one as an observer on the other's keys, but that's not necessarily the case if you're building your tab bar from templates. 实际上,通知可能是到达此处的方式-如果您将两个对象都实例化到一个位置,则可以在另一个键上将一个对象注册为观察者,但是如果您是通过模板构建选项卡栏,则不一定是这种情况。

So, notifications. 因此,通知。 The idea is, you register one view controller as a notification observer, then fire notifications from another view controller, and the observer is notified when the notification is sent. 这个想法是,您将一个视图控制器注册为通知观察者,然后从另一个视图控制器触发通知,并在发送通知时通知该观察者。 It's a little like your application sending email to itself. 就像您的应用程序向自己发送电子邮件一样。

Registering for and receiving the message looks like this: 注册和接收消息如下所示:

ViewControllerOne.m: ViewControllerOne.m:

-(void)viewDidLoad  //a likely place, but not the only place you might do this
{
    ....
    // whatever else you're doing to initialize your VC, and then

    [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(iWasNotified:)
             name:@"myNotification"
           object:nil];
}

-(void)iWasNotified:(NSNotification *)notification
{
    NSString *passedValue = (NSString *)notification.object;
    NSLog(@"We got notified and received: %@", passedValue);
}

And then sending the message is as simple as this: 然后发送消息就这么简单:

ViewControllerTwo.m: ViewControllerTwo.m:

[[NSNotificationCenter defaultCenter] 
  postNotificationName:@"myNotification"
                object:@"I'm passing you this NSString object!"];

You are obviously not limited to passing a string in the object: field. 显然,您不限于在object:字段中传递字符串。 A somewhat more likely use would be to pass self , and then you'd have access to any public fields of the notification-posting view controller. 一种更可能的用法是传递self ,然后您可以访问通知发布视图控制器的任何公共字段。

That's the bare-bones usage of them. 那是它们的基本用法。 There's a lot more subtleties you can get into, and it's all laid out here: http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Notifications/Introduction/introNotifications.html 您可以了解更多的细微之处,并且都在这里列出: http : //developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Notifications/Introduction/introNotifications.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM