[Obj-C++] 19 NSWindowの重複 : 正攻法の解決手段 / 仕上げ

[M1 Mac, Big Sur 11.6.5, clang 13.0.0, no Xcode]

AppDelegateにDrag & Dropに関連する操作を集約させて仕上げました。異常終了についても修正しました。

NSWindow初期化, NSTextField・NSView作成をAppDelegateに移管しています。

初学者にはかなりきつい内容でしたが、それなりに基礎固めできたように思います。Objective-C++を学び始めて2週間ですから進度としてはまずまずです。

#import "AppDelegate.h"
#import "DragAndDropView.h"
#import "ConvertorWindow.h"

@interface AppDelegate() <DragAndDropViewDelegate,
                            ConvertorWindowDelegate>
@property (nonatomic, assign) DragAndDropView* view_dad;
@property (nonatomic, assign) ConvertorWindow* windowInit;
@property (nonatomic, assign) NSTextField* textBox1;
@end

@implementation AppDelegate
- (void)applicationWillFinishLaunching:(NSNotification*)aNotification {

    // NSWindow
    _windowInit = [[ConvertorWindow alloc] init];
    [_windowInit autorelease];
    [_windowInit makeKeyAndOrderFront:NSApp];
	[_windowInit setTitle:@"Xlsx Convertor"];

    // NSTextField
    _textBox1 = [[[NSTextField alloc] initWithFrame:NSMakeRect(50, 265-25-10, 220, 25)] autorelease];
	[_textBox1 setStringValue:@""];
	[_textBox1 setEditable:YES];
	[[_textBox1 window] makeFirstResponder:nil];
	[[_textBox1 currentEditor] moveToEndOfLine:nil];
    [[_windowInit contentView] addSubview:_textBox1];

    // NSView
	_view_dad = [[DragAndDropView alloc] initWithFrame:NSMakeRect(0, 0, 360, 265)];
	[_view_dad setWantsLayer:NO];
	_view_dad.layer.backgroundColor = [[NSColor orangeColor] CGColor];
    [[_windowInit contentView] addSubview:_view_dad];

    NSLog(@"%s","applicationWillFinishLaunching");
}
    
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    _view_dad.delegateV = self;
    _windowInit.delegateW = self;
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
}

- (void)pasteFunction:(NSString *)fileURL;{
    NSLog(@"%s","pasteFunction kaishi");
    _textBox1.stringValue = fileURL;
    NSLog(@"%s","pasteFunction kanryou");
}

- (void)clearTextBox1;{
    _textBox1.stringValue = @"";
}

- (NSString*)getTextBox1;{
    NSString* path = [_textBox1 stringValue];
    NSLog(@"path %@",path);
    return path;
}

@end
#include <Cocoa/Cocoa.h>

@protocol ConvertorWindowDelegate <NSObject>
- (void)clearTextBox1;
- (NSString*)getTextBox1;
@end

@interface ConvertorWindow : NSWindow
@property(nonatomic, assign) id <ConvertorWindowDelegate> delegateW;
@end
<関連箇所のみ>

- (BOOL)windowShouldClose:(id)sender {
	[NSApp terminate:sender];
	return YES;
}

- (void) OnButton1Click:(id)sender {
	onoff_XlsxToList = radioButton_a1.state; 
	onoff_XlsxToCsv = radioButton_a2.state; 
	onoff_ListToXlsx = radioButton_b1.state; 
	onoff_ListToCsv = radioButton_b2.state; 
	onoff_CsvToXlsx = radioButton_c1.state; 
	onoff_CsvToList = radioButton_c2.state;

	if (radioButton_a1.state == 1){
		rbtn_num = 1;
	} else if (radioButton_a2.state == 1){
		rbtn_num = 2;
	} else if (radioButton_b1.state == 1){
		rbtn_num = 3;
	} else if (radioButton_b2.state == 1){
		rbtn_num = 4;
	} else if (radioButton_c1.state == 1){
		rbtn_num = 5;
	} else if (radioButton_c2.state == 1){
		rbtn_num = 6;
	}

	NSLog(@"case %d",rbtn_num);
	
	switch (rbtn_num){
		case 1:
		case 2:
		case 5:
		case 6:
			path = [_delegateW getTextBox1];
			convert = [[Convert alloc] init];
			result = [convert ConvertFunc:path number:rbtn_num];
			[textview setString:result];
			break;
		case 3:
		case 4:
			list = [textBox2 stringValue];
			convert = [[Convert alloc] init];
			result = [convert ConvertFunc:list number:rbtn_num];
			[textview setString:result];
			break;
	}
}

- (void) OnButton2Click:(id)sender {
	[_delegateW clearTextBox1];
	textBox2.stringValue = @"";
}