Objective c 在两个类中调用iAction方法
我试图在两个类中调用一个iAction方法,只要单击我在interface builder中创建的按钮,就会调用该方法。我真正希望的是,每当单击按钮时,NSRect都会出现,但按钮和我希望NSRect出现的位置在不同的视图中,因此,按钮位于视图A中,rect的目标位于视图B中。我曾尝试使用NSNotificationCenter执行此操作,但没有成功。它们是否在单独的窗口中?如果没有,只需在ViewB中声明iAction并将其连接到ViewA中的按钮。如果它们位于不同的窗口中,则NSNotificationCenter方式应该可以工作。您是否使用了具有相同通知名称的Objective c 在两个类中调用iAction方法,objective-c,cocoa,Objective C,Cocoa,我试图在两个类中调用一个iAction方法,只要单击我在interface builder中创建的按钮,就会调用该方法。我真正希望的是,每当单击按钮时,NSRect都会出现,但按钮和我希望NSRect出现的位置在不同的视图中,因此,按钮位于视图A中,rect的目标位于视图B中。我曾尝试使用NSNotificationCenter执行此操作,但没有成功。它们是否在单独的窗口中?如果没有,只需在ViewB中声明iAction并将其连接到ViewA中的按钮。如果它们位于不同的窗口中,则NSNotifi
postNotificationName:
和addObserver:selector:name:object
它们在不同的窗口中吗?如果没有,只需在ViewB中声明iAction并将其连接到ViewA中的按钮。如果它们位于不同的窗口中,则NSNotificationCenter方式应该可以工作。您是否使用了具有相同通知名称的postNotificationName:
和addObserver:selector:name:object
您希望有一个了解带有矩形的视图的控制器。把按钮挂在控制器上。按下按钮后,添加一个rect 您希望有一个了解带有矩形的视图的控制器。把按钮挂在控制器上。按下按钮后,添加一个rect 您缺少MVC中的C。Cocoa使用模型视图-控制器设计模式,您似乎缺少一个控制器
您应该创建一个控制器类(可能是NSWindowController
的子类,以便它负责该窗口),该类实现一个操作方法,例如连接到您的按钮的-buttonPressed:
。控制器应该管理模型(在本例中,无论矩形代表什么),这样当您按下按钮时,模型就会更新。然后,控制器应使矩形视图自身重新绘制
应该设置矩形视图,使其实现一个数据源模式(请参见NSTableView
datasource实现以获得一个好的示例),以便它知道绘制矩形的数量和位置。如果将控制器设置为视图的数据源,则视图不需要了解有关模型的任何信息
您的矩形视图应设置如下:
矩形视图.h:
@协议矩形视图数据源
@interface RectangleView : NSView
@property (weak) id <RectangleViewDataSource> dataSource;
@end
//this is the data source protocol that feeds the view
@protocol RectangleViewDataSource <NSObject>
- (NSUInteger)numberOfRectsInView:(RectangleView*)view;
- (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex;
@end
YourController.m:
#import "RectangleView.h"
@interface YourController : NSWindowController <RectangleViewDataSource>
@property (strong) NSMutableArray* rects;
@property (strong) IBOutlet RectangleView *rectView;
- (IBAction)buttonPressed:(id)sender;
@end
@implementation YourController
@synthesize rects;
@synthesize rectView;
- (id)init
{
self = [super init];
if (self)
{
rects = [[NSMutableArray alloc] init];
}
return self;
}
- (void)awakeFromNib
{
//assign this controller as the view's data source
self.rectView.dataSource = self;
}
- (IBAction)buttonPressed:(id)sender
{
NSRect rect = NSMakeRect(0,0,100,100); //create rect here
[rects addObject:[NSValue valueWithRect:rect]];
[self.rectView setNeedsDisplay:YES];
}
//RectangleViewDataSource methods
- (NSUInteger)numberOfRectsInView:(RectangleView*)view
{
return [rects count];
}
- (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex
{
return [[rects objectAtIndex:anIndex] rectValue];
}
@end
您缺少MVC中的C。Cocoa使用模型视图-控制器设计模式,您似乎缺少一个控制器 您应该创建一个控制器类(可能是
NSWindowController
的子类,以便它负责该窗口),该类实现一个操作方法,例如连接到您的按钮的-buttonPressed:
。控制器应该管理模型(在本例中,无论矩形代表什么),这样当您按下按钮时,模型就会更新。然后,控制器应使矩形视图自身重新绘制
应该设置矩形视图,使其实现一个数据源模式(请参见NSTableView
datasource实现以获得一个好的示例),以便它知道绘制矩形的数量和位置。如果将控制器设置为视图的数据源,则视图不需要了解有关模型的任何信息
您的矩形视图应设置如下:
矩形视图.h:
@协议矩形视图数据源
@interface RectangleView : NSView
@property (weak) id <RectangleViewDataSource> dataSource;
@end
//this is the data source protocol that feeds the view
@protocol RectangleViewDataSource <NSObject>
- (NSUInteger)numberOfRectsInView:(RectangleView*)view;
- (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex;
@end
YourController.m:
#import "RectangleView.h"
@interface YourController : NSWindowController <RectangleViewDataSource>
@property (strong) NSMutableArray* rects;
@property (strong) IBOutlet RectangleView *rectView;
- (IBAction)buttonPressed:(id)sender;
@end
@implementation YourController
@synthesize rects;
@synthesize rectView;
- (id)init
{
self = [super init];
if (self)
{
rects = [[NSMutableArray alloc] init];
}
return self;
}
- (void)awakeFromNib
{
//assign this controller as the view's data source
self.rectView.dataSource = self;
}
- (IBAction)buttonPressed:(id)sender
{
NSRect rect = NSMakeRect(0,0,100,100); //create rect here
[rects addObject:[NSValue valueWithRect:rect]];
[self.rectView setNeedsDisplay:YES];
}
//RectangleViewDataSource methods
- (NSUInteger)numberOfRectsInView:(RectangleView*)view
{
return [rects count];
}
- (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex
{
return [[rects objectAtIndex:anIndex] rectValue];
}
@end
哦,等等,尽管它可以工作,但NSRect没有绘制,现在Xcode表示属性数据源不符合协议。您知道如何修复此问题吗?您是否确保控制器在其方法声明中声明它符合协议
@interface YourController:NSWindowController
如果看不到您的实际操作,我无法帮助您绘制矩形。可能最好在这个问题上用您的实际源代码发布另一个问题。但是您知道为什么属性数据源不符合协议,而我的控制器类则不符合协议吗?哦,等等,不要紧,它确实可以工作,但NSRect没有绘制,现在Xcode表示属性数据源不符合协议。您知道如何修复此问题吗?您是否确保控制器在其方法声明中声明它符合协议@interface YourController:NSWindowController
如果看不到您的实际操作,我无法帮助您绘制矩形。也许最好在这个问题上用您的实际源代码发布另一个问题。但是您知道为什么属性数据源不符合协议,而我的控制器类则不符合协议吗?