双击NSTableView中的行不显示新视图

use*_*624 9 cocoa objective-c nstableview

我有一个使用核心数据的os x应用程序.

我的应用程序中有3个.xib文件,它们是:

1. MainMenu.xib
2. MasterTableViewController.xib 
3. DetailViewController.xib  
Run Code Online (Sandbox Code Playgroud)

启动时,app会显示一个包含NSTableView的视图,其中包含几条记录.

我将该视图命名为MasterTableViewController

我希望当用户双击该行时,隐藏"主"视图并显示我的"详细信息"视图.我将该视图命名为DetailViewController.

双击"主"视图中NSTableView中的行时,没有任何反应,"主"视图仍然可见.我想要的是"主"视图消失,并且"细节"视图出现.

这是我现在的代码,更多解释如下:

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;


    @property (nonatomic,strong) NSViewController *mainAppViewController;
    @property (weak) IBOutlet NSView *mainAppView;
    - (void)changeViewController:(NSInteger)tag;

    @property (weak) IBOutlet NSTableView *websitesTableView;
    - (void)tableViewDoubleClick:(id)nid;

@end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

#import "AppDelegate.h"
#import "MasterTableViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()

    @property (weak) IBOutlet NSWindow *window;
    - (IBAction)saveAction:(id)sender;

@end

@implementation AppDelegate

    NSString *const masterTable = @"MasterTableViewController";
    NSString *const detail = @"DetailViewController";


    -(void)awakeFromNib {   
        [_websitesTableView setTarget:self];
        [_websitesTableView setDoubleAction:@selector(tableViewDoubleClick:)];
    }

    - (void)tableViewDoubleClick:(id)nid {
        NSInteger rowNumber = [_websitesTableView clickedRow];
        NSTableColumn *column = [_websitesTableView tableColumnWithIdentifier:@"websiteUrl"];
        NSCell *cell = [column dataCellForRow:rowNumber];

        NSInteger tag = 2;
        [self changeViewController:tag];
    }

    - (void)changeViewController:(NSInteger)tag {

        [[_mainAppViewController view]removeFromSuperview];  
        switch (tag) {
            case 1:
                self.mainAppViewController = [[MasterTableViewController alloc]initWithNibName:masterTable bundle:nil];
            break;

            case 2:
                self.mainAppViewController = [[DetailViewController alloc]initWithNibName:detail bundle:nil];
            break;

         }

    [_mainAppView addSubview:[_mainAppViewController view]];
    [[_mainAppViewController view] setFrame:[_mainAppView bounds]];
    [[_mainAppViewController view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    }


    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // automatically run the master table view controller
        NSInteger tag = 1;
        [self changeViewController:tag];
    }
Run Code Online (Sandbox Code Playgroud)

现在,你们中的一些人可能想知道,其余的代码在哪里.我已经在AppDelegage.m中省略了下面核心数据的样板代码,因为它没有变化.我使用绑定来使我的NSTableView工作并显示我的记录,因此MasterTableViewController.h和.m文件是空的,对于DetailViewController.h和.m文件也是如此.

重要提示 - 我在这里无法理解:如果我在applicationDidFinishLaunching方法中更改了2中的标记,则会正常显示详细视图,但如果我将其重新打开1,然后双击该行,则"主"视图(使用NSTableView)仍然可见,并且没有任何反应(视图未被交换)

任何人都可以帮我找出我的代码有什么问题?

问候,约翰

Ken*_*ses 3

AppDelegate显然,您在 MasterTableViewController.xib 文件中实例化了类的第二个实例。应该只有一个AppDelegate实例,即 MainMenu.xib 中的实例。因此,它不应该位于 MasterTableViewController.xib 中。

其中一个实例正在从表中接收双击操作方法,但另一个实例是具有主窗口出口的实例。

您需要摆脱第二个实例并找到另一种方法来从MasterTableViewController.