以下是我将如何做到这一点。我不会创建一个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
)。
然后在您想要加载通用自定义视图的其他类中,像这样实例化通用视图控制器。
#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
}
希望我可以在这个问题上设置赏金。我真的想知道它是否可能。 –