委托和多个视图控制器的初始化顺序不起作用

问题描述 投票:0回答:1

我正在尝试构建一个程序,其中 MainController 使用委托将数据传递到 TableViewController 内的表中。我认为我已经正确初始化了它,但 LoadSN() 没有触发委托函数(我确实看到它运行)。 MainController 和 TableViewController 的初始化不能很好地配合吗? (也许我不明白初始化的顺序?)

主控制器.h

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN
@class MainController;
@protocol MainDelegate <NSObject>

- (void)addMainEntry:(NSString *)patchName :(NSString *)filename;

@end
@interface MainController : NSWindowController
{
@private
    IBOutlet NSTextField *status;
    IBOutlet NSTextField *serialNumber;
}
@property (nonatomic, weak) id<MainDelegate> delegate;
-(IBAction)LoadSNBtn:(id)sender;
-(IBAction)saveBtn:(id)sender;

@end
NS_ASSUME_NONNULL_END

主控制器.m

#import "MainController.h"

@implementation MainController

@synthesize delegate;

- (void)awakeFromNib
{
    //NSLog(@"%@",[status stringValue]);
    NSLog(@"awakeFromNib MainController");
    NSString* msg = @"Main Station: Scan or Enter Serial Number above.";
    msg = [NSString stringWithFormat:@"%@\n%@",[status stringValue], msg];
    [status setStringValue:msg];
    [status setDrawsBackground:YES];
    [status setSelectable:YES];
}
- (void)windowDidLoad {
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
- (IBAction)LoadSNBtn:(id)sender
{
    NSLog(@"LoadSNBtn");
    [delegate addMainEntry:@"LoadName" :@"LoadFilename"];
}
- (IBAction)saveBtn:(id)sender
{
    
}
@end

TableViewController.h

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "MainController.h"
#import "mainImage.h"

NS_ASSUME_NONNULL_BEGIN

@interface TableViewController : NSObject <NSTableViewDataSource, MainDelegate>
{
    @public
    IBOutlet NSTableView *tableView;
    NSMutableArray *imageList;
}
@property (copy) NSMutableArray *imageList;
@property (weak) MainController *mainController;
@end

NS_ASSUME_NONNULL_END

TableViewController.m #导入“TableViewController.h”

@implementation TableViewController
@synthesize imageList = _imageList;
@synthesize mainController = _mainController;

-(id)init
{
    self = [super init];
    if (self)
    {
        NSLog(@"tableViewController init");
        [_mainController setDelegate:self];
        _imageList = [[NSMutableArray alloc] init];
        mainImage *mainEntry = [[mainImage alloc] init];
        [mainEntry setValue:@"noFilename" forKey:@"filename"];
        
        [_imageList addObject:mainEntry];

    }
    return self;
}
- (void)awakeFromNib
{
    NSLog(@"awakeFromNib TableViewController");

}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{   // this needs to return the number of rows that the array has so tableView can display all of them
    //NSLog(@"size: %i", [list count]);
    return [_imageList count];
    
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{   // need to pass in table column and row and return the right data to display
    mainImage *entry = [_imageList objectAtIndex:row];
    NSString *identifier = [tableColumn identifier]; // need to find the column, either name/age
    //NSLog(@"identifier: %@", identifier);
    return [entry valueForKey:identifier];
}
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
    mainImage *entry = [_imageList objectAtIndex:row];
    NSString *identifier = [tableColumn identifier];
    
    [entry setValue:object forKey:identifier];
}
-(void)addMainEntry:(NSString *)patchName :(NSString *)filename
{
    NSLog(@"inside adding main entry");
    [_imageList addObject:[[mainImage alloc] init]];
    mainImage *entry = [_imageList objectAtIndex:0];
    [entry setValue:patchName forKey:@"patchName"];
    [entry setValue:filename forKey:@"filename"];
    //NSLog(@"name: %@ filename: %@", [entry valueForKey:@"patchName"], [entry valueForKey:@"filename"]);
    
    [tableView reloadData];
}
@end
objective-c xcode cocoa delegates
1个回答
0
投票

这看起来像 TableViewController 是从 Storyboard 或 XIB 加载的(给定 IBOutlet 注释)。在这种情况下,不会调用

init
。您需要将逻辑放入
awakeFromNib
中,就像对 MainController 所做的那样。

© www.soinside.com 2019 - 2024. All rights reserved.