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



我有一个使用core data的os x应用程序。

我的app中有3个。xib文件,它们是:

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

当启动时,应用程序显示一个视图,其中有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

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

现在,你们中的一些人可能想知道,剩下的代码在哪里。我已经在appdelegate中提交了下面核心数据的样板代码。M,因为它是不变的。我使用绑定使我的NSTableView工作,并显示我的记录,所以MasterTableViewController.h和。m文件是空的,同样是真实的DetailViewController.h和。m文件。

重要的注意-什么我不能理解这里:如果我改变标签2在applicationDidFinishLaunching方法,详细视图显示正常,但如果我切换回1,然后双击行,"主"视图(与NSTableView)仍然可见,什么也没有发生(视图没有交换)

谁能帮我找出我的代码有什么问题?

问候,约翰。

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

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

你需要(ed)摆脱第二个实例,并找到另一种方式从MasterTableViewController访问应用程序委托。

最新更新