objective-c - WindowController、IBOutlets 和面向对象编程?

标签 objective-c cocoa class nswindowcontroller

对于主题行缺乏具体性,我深表歉意,但我不完全确定如何对我的问题进行分类。这是一个高水平的问题,但我觉得人们必须一直遇到这个问题,我想知道他们如何处理这个问题。我对 Objective C 和面向对象编程来说是一个相对菜鸟,所以如果这对你们很多人来说都是显而易见的,请原谅我。

就这样吧。我有一个 cocoa 应用程序,它在 mainMenu.xib 中创建主窗口和主应用程序 Controller 。我的 myMainAppController 对象包含另一个 windowController,例如 mySubWindowController,它是从其自己的单独 nib 文件启动的,如下所示。假设子窗口有两个元素,可能是 NSTextfield 和 NSButton。所以...

myMainAppController.h
@interface
   @property (strong) MySubWindowContorller *mySubWindowController;
   ........ so forth


MyMainAppController.m
@implementation
 ......
 self.mySubWindowController = [MySubWindowController alloc] initWithWindowNibName:
 @"mySubWindow"];  
 etc...


MySubWindowController.h
@ interface
  IBOutlet (weak) NSTextfield *myTextField;
  IBOutlet (weak) NSButton *myButton;
....

到目前为止,我认为一切都很好。相当标准的东西,对吧?这就是我问题的关键。在我的类的这种结构下,如何将子窗口中发生的任何信息或事件返回到我的 mainAppController 以及从 mainAppController 到子窗口的数据?我似乎无法将 IBOutlet 或 IBAction 从文本字段/按钮返回到 myMainAppController.h,因此除了使用 KVO 之外,myMainAppController 将如何从 mySubWindowController 获取任何信息?如果子窗口中实现的操作需要 myMainWindowController 的元素怎么办?我无法将操作消息发送回 myMainAppController 并且 mySubWindowController 无法访问其包含类的其他元素。我想我可以在 myMainAppController 中声明并定义所需元素的软指针到 mySubWindowController,但这在某种程度上似乎违反了“面向对象”和本地化。

当主窗口和子窗口需要协调数据和逻辑时,你会怎么做?由于我的经验不足,我是否错过了一些完全明显的事情,或者这种情况是否相当常见?在我开发应用程序的相对较短的时间内,我已经遇到过几次这种情况。试图了解其他人如何处理这个问题..

预先感谢您的任何想法。

最佳答案

为此,您必须编写自己的自定义委托(delegate)。希望下面的代码对您有帮助。其中有两个窗口 Controller ,假设 AwindowController 和 BwindowController。如果 AwindowController 上发生任何事件,BwindowController 就会知道。例如,我在 AwindowController 中创建了一个按钮,如果我们单击该按钮,则 BwindowController 将收到单击 AwindowController 按钮时的一些消息或数据。就像这样,您可以发送任何您想要发送的数据或字符串值。 注意:- 客户委托(delegate)已在 BWindowController 中编写

#import "sampleDelegate.h"
#import <Cocoa/Cocoa.h>
#import "BWindowController.h"

@interface AWindowController : NSWindowController<sampleDelegate>

{
    NSString *text;
}
@property(readwrite,retain)NSString *text;
-(IBAction)doSet:(id)sender;

@end

#import "AWindowController.h"
#import "BWindowController.h"

@interface AWindowController ()

@end

@implementation AWindowController
@synthesize text;


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }

    return self;
}

-(NSString*)getDataValue
{
    return @"Button clicked on AWindow";
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

-(NSString*)windowNibName
{
    return @"AWindowController";
}

-(IBAction)doSet:(id)sender
{
    [self setText:[self text]];
    BWindowController *b=[[BWindowController alloc]init];
    b.delegate=self;
    [b showWindow:self];
}
@end

#import <Cocoa/Cocoa.h>
#import "sampleDelegate.h"

@protocol sampleDelegate <NSObject>
@required
-(NSString *)getDataValue;
@end
@interface BWindowController : NSWindowController<sampleDelegate>
{
    NSString *bTextValue;
    id<sampleDelegate>delegate;
}
@property(readwrite,retain)NSString *bTextValue;
@property(readwrite,assign)id<sampleDelegate>delegate;
@end

#import "BWindowController.h"
@interface BWindowController ()

@end

@implementation BWindowController
@synthesize bTextValue,delegate;
- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }

    return self;
}
-(NSString *)getDataValue
{
    return nil;
}
- (void)windowDidLoad
{
   NSString *str= [[self delegate]getDataValue];
    [self setBTextValue:str];
    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

-(NSString*)windowNibName
{
    return @"BWindowController";
}
@end

关于objective-c - WindowController、IBOutlets 和面向对象编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085858/

相关文章:

C# 一般设置属性

java - 如何使用循环来创建新对象?

iphone - 像app这样实现网页有哪些坑?

cocoa - 设置 TableView 的数据源时无法分配给此表达式的结果

objective-c - 我可以检测到: "Does class overload method of base class"?吗

在 OS X Swift 应用程序中单击按钮时 Xcode 打印 "Hello World"的方法?

objective-c - 基于 NSStrings 的 NSTableView 行高

html - 如何覆盖内容 div 中的 CSS 类

iphone - 在 UIView 中存储额外的关联信息

iOS 共享扩展不显示我的自定义 ShareViewController