我試圖在兩個類中調用一個IBAction方法,每當我在界面構建器中創建的按鈕被單擊時調用它。我真正想要發生的是每當單擊按鈕時出現NSRect,但是按鈕和我希望NSRect出現的位置處於單獨的視圖中,因此該按鈕位於視圖A中,且矩形的目標位於查看B.我曾嘗試用NSNotificationCenter做這件事,但它沒有奏效。在兩個類中調用IBAction方法
0
A
回答
2
您錯過了C在MVC。可可使用模型 - 查看-控制器設計模式,你似乎缺少一個控制器。
您應該創建一個控制器類(可能是NSWindowController
的一個子類,以便它負責窗口),該類實現了一個操作方法,如-buttonPressed:
連接到您的按鈕。控制器應該管理模型(在這種情況下,無論矩形代表什麼),以便當您按下按鈕時,模型會更新。然後控制器應該讓你的矩形視圖重繪自己。
應該設置您的矩形視圖,以便它實現數據源模式(請參閱NSTableView
數據源實現的一個很好的示例),以便知道有多少個矩形以及在哪裏繪製矩形。如果將控制器設置爲視圖的數據源,則視圖不需要知道有關模型的任何信息。
你的矩形認爲應建立這樣的:
RectangleView.h: @protocol RectangleViewDataSource;
@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
RectangleView.m:
@implementation RectangleView
@synthesize dataSource;
- (void)drawRect:(NSRect)rect
{
//only draw if we have a data source
if([dataSource conformsToProtocol:@protocol(RectangleViewDataSource)])
{
//get the number of rects from the controller
NSUInteger numRects = [dataSource numberOfRectsInView:self];
for(NSUInteger i = 0; i < numRects; i++)
{
NSRect currentRect = [dataSource rectangleView:self rectAtIndex:i];
//draw the rect
NSFrameRect(currentRect);
}
}
}
@end
你會指定你的控制器作爲視圖的數據源,並使其實現RectangleViewDataSource
協議的方法。
控制器會是這個樣子:
YourController.h:
#import "RectangleView.h"
@interface YourController : NSWindowController <RectangleViewDataSource>
@property (strong) NSMutableArray* rects;
@property (strong) IBOutlet RectangleView *rectView;
- (IBAction)buttonPressed:(id)sender;
@end
YourController.m:
@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
1
他們在單獨的窗口?如果它們不是,只需在ViewB中聲明一個IBAction並將它連接到ViewA中的按鈕即可。如果他們在不同的窗口,NSNotificationCenter的方式應該工作。您是否使用postNotificationName:
和addObserver:selector:name:object
以及相同的通知名稱?
1
你想擁有一個控制器,通過rects知道視圖。將按鈕掛到控制器上。按下按鈕時,添加一個矩形。
相關問題
- 1. 從另一個類調用(IBAction)方法
- 2. 調用IBAction方法
- 3. IBAction方法不調用viewDidLoad方法?
- 4. 如何調用一個IBAction爲方法從另一個方法
- 5. 子類NSControl,IBAction不在Swift中調用
- 6. 在JSF中調用兩個Bean方法
- 7. 類擴展中的IBAction方法?
- 8. UIBarButton項目兩種方法/ IBAction爲
- 9. 如何使用IBAction方法調用加速器方法?
- 10. 完成處理程序在IBAction方法中調用
- 11. 在同一個類中調用方法
- 12. 在父類中調用一個方法
- 13. 在Java中,可以合併兩個類,以便兩個類的方法都可以從一個類中調用?
- 14. 如何從 - (IBaction)方法調用 - (void)方法?
- 15. iOS7也從ViewDidLoad調用一個IBAction方法
- 16. 調用一個IBAction爲
- 17. 如何將該類的IBAction方法轉換爲另一個類
- 18. 使用另一個UIViewController的IBAction調用一個UIControllerView的IBAction iOS
- 19. 如何在IBAction中實現NSArray方法?
- 20. 如何在onCreate方法中調用另一個類的方法?
- 21. 兩個線程調用一個方法
- 22. 從Python 2.7中的另一個類方法調用類方法
- 23. IPHONE:UIAlertView在自定義函數中調用兩次/ IBAction
- 24. 設置UISwitch isOn以編程方式從IBAction再次調用IBAction
- 25. 在同一個類的方法定義中調用一個類方法
- 26. 如何在一個類中調用另一個類的方法
- 27. 在另一個類中調用一個類的方法?
- 28. 找出在另一個類中調用哪個類的方法
- 29. Ruby中的子類方法在父類中調用方法
- 30. viewDidLoad在IBAction之前調用?
謝謝!它現在有效! –
哦,別再介意它可以工作,但NSRect不會畫圖 –
現在Xcode說屬性數據源不符合協議。你知道如何解決這個問題嗎? –