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

时间:2015-08-13 10:47:56

标签: objective-c cocoa nstableview

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

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

1. MainMenu.xib
2. MasterTableViewController.xib 
3. DetailViewController.xib  

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

我将该视图命名为MasterTableViewController

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

在" master"中双击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

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];
    }

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

重要提示 - 我在此无法理解:如果我在applicationDidFinishLaunching方法中更改了2中的标记,则会正常显示详细视图,但如果我将其切换回1,然后双击该行,&#34; master&#34;视图(使用NSTableView)仍然可见,并且没有任何事情发生(视图未被交换)

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

问候,约翰

1 个答案:

答案 0 :(得分:3)

您显然在MasterTableViewController.xib文件中实例化了AppDelegate类的第二个实例。应该只有一个AppDelegate实例,而这是MainMenu.xib中的实例。所以,它不应该在MasterTableViewController.xib中。

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

您需要(ed)摆脱第二个实例并找到另一种方法来从MasterTableViewController访问应用代理。

相关问题