我无法从自定义视图bView调用用UIViewController(aViewController)编写的方法。
我想在UIView(bView)中单击按钮(btn)调用方法函数。但是,当我单击按钮时,出现以下错误。
无法识别的选择器已发送到实例...。
这是我的代码:
aViewController.h
#import "bView.h"
#import "cViewController.h"
-(void)function;
aViewController.m
-(void)viewDidLoad{
bView = [[bView alloc]init];
[bView makeView];
bView.bViewController = self;
[self.view addSubView:bView];
}
-(void)function{
cViewController *nextViewController = [[cViewController alloc] initWithNibName: nil bundle: nil];
[self.navigationController pushViewController: nextViewController animated: YES];
}
bView.h
#import "bViewController.h"
@interface bView : UIView{
UIButton btn;
bView *BView;
}
-(void)makeView;
-(void)function;
@property (nonatomic,readonly) UIButton *btn;
@property (assign) UIViewController* aViewController;
浏览器
#import "bView.h"
#import "aViewController.h"
@impementation bView
@synthesize btn;
@synthesize aViewController;
-(void)makeView{
btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *btnimg = [UIImage imageNamed:@"btn.png"];
[btn setBackgroundImage:btnimg forState:UIControlStateNormal];
btn.frame = CGRectMake(65, 380, 60, 60);
[self addSubview:btn];
btn.userInteractionEnabled = YES;
[btn addTarget:aViewController action:@selector(function) forControlEvents:UIControlEventTouchUpInside];
}
怎么解决呢?
最佳答案
您正在将目标设置为[bView makeView];
方法中的按钮,但是那时bView
的aViewController
为零,因为您仅在下一行进行了设置。所以试试这个:
-(void)viewDidLoad{
bView = [[bView alloc]init];
bView.bViewController = self;
[bView makeView];
[self.view addSubView:bView];
}