我只是喜欢玩编码爱好,所以可能是noob问题;Objective-c - 其他类的控制插座
我有一个简单的MacOS视图板,有2个视图。两者都有自己的类(主类和子类)。我如何控制主类的子类中的插座?
例如
我在mainclass的按钮(IBAction为),并在子类中一个文本字段(IBOutlet中)。我想通过点击main中的按钮来设置文本字段的stringvalue。
我已经搜索了很多最后的日子,但只是不明白。 (或者只需要在正确的方向推)
编辑JingJingTao的回答后:
我使用的控制拖动功能打开第二个窗口。 我试了JingJingTao给出的代码,但是textfield没有响应这个动作。 我的类看起来现在这个样子:
ViewController.h
#import <Cocoa/Cocoa.h>
@interface ViewController : NSViewController
- (IBAction)newText:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController()
@property (nonatomic) ViewController2 *subclass;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
- (void)newText:(id)sender {
self.subclass.textField.stringValue = @"button pressed";
}
@end
ViewController2.h
#import "ViewController.h"
@interface ViewController2 : ViewController
@property (nonatomic) IBOutlet NSTextField *textField;
@end
ViewController2.m
#import "ViewController2.h"
@interface ViewController2()
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
你不能。每个场景都是一个单独的笔尖。 – matt
您需要对拥有插座的类的实例进行引用。但是,这两个观点同时在屏幕上?或者你真的想在某处设置一些标志/状态以便以后使用... – Wain
它们同时在屏幕上 –