我有一个名为BackgroundView1的小视图对象,它是NSView的一个子类,坐在MainMenu.xib上。主视图控制器是AppDelegate。该视图对象显示一些图形。无论如何,我有这个视图对象的下面的代码。从ViewController强制刷新NSView(AppDelegate)
// .h
@interface BackgroundView1 : NSView
// .m
@implementation BackgroundView1
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:[self window]];
}
- (void) windowDidBecomeKey:(NSNotification *)notification {
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)rect {
// ...
// ...
}
我不知道是否有强行从AppDelegate中刷新此视图对象(BackgroundView1)的任何方式,当用户点击一个按钮?我在AppDelegate中也有以下代码。
// AppDelegate.h
#import "BackgroundView1.h"
@interface AppDelegate : NSObject
@property (weak) BackgroundView1 *view1;
// AppDelegate.m
- (IBAction)button1Clicked:(id)sender {
BackgroundView1 *view1 = [[BackgroundView1 alloc] init];
[view1 setNeedsLayout:YES];
}
感谢您的帮助。