- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于主题行缺乏具体性,我深表歉意,但我不完全确定如何对我的问题进行分类。这是一个高水平的问题,但我觉得人们必须一直遇到这个问题,我想知道他们如何处理这个问题。我对 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/
我能够找到当前打开的窗口: NSWindow *currentWindow = [NSApp keyWindow]; 但是我怎么知道哪个类控制这个窗口,即上面窗口的窗口 Controller ? 最佳
对于主题行缺乏具体性,我深表歉意,但我不完全确定如何对我的问题进行分类。这是一个高水平的问题,但我觉得人们必须一直遇到这个问题,我想知道他们如何处理这个问题。我对 Objective C 和面向对象编
谁能告诉我如何将位置 X 和 Y 返回到我的 WindowController NSTextField(testTextX 和 testTextY)? 我以编程方式创建了一个 windowContro
我被告知 awakeFromNib 应该只被调用一次,所以我很惊讶地看到它被我的 WindowController 调用了两次。我使用 IB 在 MainMenu.xib 中创建 WindowCont
当您第一次在 XCode 中创建应用程序时,它会在最低级别创建 AppDelegate.h/.m 和 MainWindow.xib 一旦您开始修改 Xib 以最适合您的设计需求 - 最好的做法是不再将
下面的代码在 2 年前运行 今天我尝试重建 AppDelegate.h #import #include #include #include @class EnterWindowControl
在我的 cocoa OS X 应用程序中,我有一个带有 xib 文件的WindowController,以及两个 ViewControllers使用 xib 文件,我在 WindowControlle
我正在尝试将一些旧代码转换为 ARC。旧代码在 WindowController 中执行此操作: @interface PreferencesController () @end @implemen
我在窗口 Controller 文件中为 NSTextView 创建了一个 socket 属性。然后我创建了一个单独的窗口 xib 文件,其中包含 NSTextView。如何将 xib 文件中的 Vi
在我的应用程序委托(delegate)中,我创建了一个 NSManagedObjectContext 属性,我想将其传递给我在 applicationDidFinishLaunching: 中启动的窗
我是一名优秀的程序员,十分优秀!