这就是我将如何做到的。我不会创建一个 CustomNSView,我会创建一个 CustomViewController(包括它的 xib 文件)。在那个 CustomViewController 上,我会设计两个按钮并像这样设置 CustomViewController.h。
@property (nonatomic, weak) id delegate; // Create a delegate to send call back actions
-(IBAction)buttonOneFromCustomVCClicked:(id)sender;
-(IBAction)buttonTwoFromCustomVCClicked:(id)sender;
CustomViewController.m 就是这样。
-(void)buttonOneFromCustomVCClicked:(id)sender {
if ([self.delegate respondsToSelector:@selector(buttonOneFromCustomVCClicked:)]) {
[self.delegate buttonOneFromCustomVCClicked:sender];
}
}
-(void)buttonTwoFromCustomVCClicked:(id)sender {
if ([self.delegate respondsToSelector:@selector(buttonTwoFromCustomVCClicked:)]) {
[self.delegate buttonTwoFromCustomVCClicked:sender];
}
}
在 customViewController 的界面构建器中,将两个按钮的SentAction
事件链接到两个方法(它们应该显示在 中file's owner
)。
然后在要加载通用自定义视图的 otherClass 中,像这样实例化通用视图控制器。
#import "customViewController.h"
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
customViewController *newCustomViewController = [[ViewController alloc] initWithNibName:@"customViewController" bundle:nil];
[newCustomViewController setDelegate:self];
self.backGroundView = [newCustomViewController view]; // Assuming **backGroundView** is an image view on your background that will display the newly instantiated view
}
-(void)buttonOneFromCustomVCClicked:(id)sender {
// Code for when button one is clicked
}
-(void)buttonTwoFromCustomVCClicked:(id)sender {
// Code for when button two is clicked
}