本文介绍了Mac OSX故事板:NSViewController之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OS X可可应用程序项目中使用情节提要,其中包含SplitView控制器和其他2个视图控制器LeftViewController和RightViewController.

I use storyboard in a OS X cocoa application project with a SplitView controller and 2 others view controller LeftViewController and RightViewController.

在LeftViewController中,我有一个tableView显示一个名称数组. tableview的数据源和委托是LeftViewController.

In the LeftViewController i have a tableView that display an array of name. The datasource and delegate of the tableview is the LeftViewController.

在RightViewController中,我只是有一个居中的标签,用于显示选择名称.我想在右视图中显示在左视图中选择的名称.

In the RightViewController i just have a centered label that display the select name. I want to display in the right view the name selected in the left view.

要配置2个视图控制器之间的通信,我使用AppDelegate,并在AppDelegate.h中为每个控制器定义2个属性.2属性是使用NSInvocation波纹管在视图控制器的viewDidLoad中初始化的:

To configure the communication between the 2 views controllers i use the AppDelegate and i define 2 property for each controller in AppDelegate.hThe 2 property are initialized in the viewDidLoad of view controller using the NSInvocation bellow :

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    id delg = [[NSApplication sharedApplication] delegate];
    SEL sel1 = NSSelectorFromString(@"setRightViewController:");
    NSMethodSignature * mySignature1 = [delg methodSignatureForSelector:sel1];
    NSInvocation * myInvocation1 = [NSInvocation
                                    invocationWithMethodSignature:mySignature1];

    id me = self;

    [myInvocation1 setTarget:delg];
    [myInvocation1 setSelector:sel1];
    [myInvocation1 setArgument:&me atIndex:2];
    [myInvocation1 invoke];
}

我在LeftViewController中有相同的内容.

I have the same in LeftViewController.

然后,如果我在表视图中单击一个名称,则会向该代理发送一条消息,并在参数中使用该名称,然后该代理使用给定名称更新RightViewController的标签.效果不错,但根据苹果的最佳实践,效果不好.

Then if i click on a name in the table view, i send a message to the delegate with the name in parameter and the delegate update the label of the RightViewController with the given name. It works fine but according to apple best practice it’s not good.

在情节提要中的2个视图控制器之间还有另一种通信方式吗?

Is there another way to communicate between 2 view controller inside a storyboard ?

我已经阅读了很多文章,但对于OS X却一无所获.

I've already read a lot of post but found nothing for OS X.

您可以在此处下载简单的项目: http://we.tl/4rAl9HHIf1

You can download the simple project here : http://we.tl/4rAl9HHIf1

推荐答案

我设法通过在AppDelegate.m中添加以下代码来获得想要的东西:

I managed to get what i want by adding the following code in AppDelegate.m :

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    //
    NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main"
                                                            bundle:[NSBundle mainBundle]];

    self.windowController = [storyboard instantiateControllerWithIdentifier:@"windowController"];
    self.window = self.windowController.window;

    self.splitViewController = (NSSplitViewController*)self.windowController.contentViewController;
    NSSplitViewItem *item0 = [self.splitViewController.splitViewItems objectAtIndex:0];
    NSSplitViewItem *item1 = [self.splitViewController.splitViewItems objectAtIndex:1];
    self.leftViewController = (OMNLeftViewController*)item0.viewController;
    self.rightViewController = (OMNRightViewController*)item1.viewController;

    [self.window makeKeyAndOrderFront:self];
    [self.windowController showWindow:nil];
}

我们还需要如下编辑故事板NSWindowController对象:

We also need to edit the storyboard NSWindowController object as follow :

取消选中是初始控制器"复选框,因为我们以编程方式将其添加到AppDelegate.m中.

Uncheck the checkbox 'Is initial controller' because we add it programmatically in AppDelegate.m.

现在,左右视图可以进行通讯了.只需在OMNLeftViewController.h中定义一个名为rightView的属性即可:

Now the left and right view can communicate. Just define a property named rightView in OMNLeftViewController.h :

    self.leftViewController.rightView = self.rightViewController;

这篇关于Mac OSX故事板:NSViewController之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 15:00